// 唯一id
let pageId = 1;

export default {
  namespace: 'page',

  state: {
    active: [],
    zoomOutList: [],
  },

  effects: {},

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

    add(state, { payload: { key, params }, callback }) {
      pageId++;
      const newActive = [
        ...state.active,
        {
          key,
          params,
          id: pageId,
        },
      ];
      try {
        callback?.(pageId);
      } catch (error) {
        console.error(error);
      }
      return {
        ...state,
        active: newActive,
      };
    },
    remove(state, { payload: { id, key } }) {
      const { active } = state;
      let index = active.length - 1;
      if (key) {
        index = active.findIndex((item) => item.key == key);
      } else if (id) {
        index = active.findIndex((item) => item.id == id);
      }
      if (index != -1) {
        active.splice(index, 1);

        return {
          ...state,
          active: [...active],
        };
      }
      return state;
    },
    removeByKey(state, { payload: { key } }) {
      const { active } = state;
      let newAcitve = active.filter((item) => item.key != key);

      if (newAcitve.length != active.length) {
        return {
          ...state,
          active: [...newAcitve],
        };
      }
      return state;
    },
    replace(state, { payload: { key, params, id } }) {
      const { active } = state;
      pageId++;

      let index = active.length - 1;
      if (id) index = active.findIndex((item) => item.id == id);
      if (index != -1) {
        let oldActive = active[index];
        // 记录跳转历史
        let history = oldActive.history || [];
        let newActive = {
          key,
          params,
          id: pageId,
          history: [...history, oldActive],
        };
        active[index] = newActive;

        return {
          ...state,
          active: [...active],
        };
      }
      return state;
    },
    back(state, { payload: { id } }) {
      const { active } = state;
      pageId++;

      let index = active.length - 1;
      if (id) index = active.findIndex((item) => item.id == id);
      if (index != -1) {
        let currentActive = active[index];
        // 从history中取出上一次的active
        let history = currentActive.history || [];
        let priveActive = history.pop();
        const { key, params, id } = priveActive;

        // 设置为当前active
        active[index] = {
          key,
          params,
          id,
          history: [...history],
        };

        return {
          ...state,
          active: [...active],
        };
      }
      return state;
    },
    zoomOut(state, { payload: { index, options } }) {
      const { zoomOutList, active } = state;
      const activeItem = active[index];

      let newAcitve = [...active];
      newAcitve.splice(index, 1);

      return {
        ...state,
        active: newAcitve,
        zoomOutList: [
          ...zoomOutList,
          {
            ...activeItem,
            options,
          },
        ],
      };
    },
    zoomIn(state, { payload: { index } }) {
      const { zoomOutList, active } = state;
      let newZoomList = [...zoomOutList];
      let activeItem = newZoomList.splice(index, 1)[0];

      return {
        ...state,
        zoomOutList: newZoomList,
        active: [...active, activeItem],
      };
    },
    setActive(state, { payload: { index } }) {
      const { active } = state;
      // 判断是否需要操作
      if (index == active.length - 1) return state;
      const newActive = [...active];
      let activeItem = newActive.splice(index, 1)[0];
      // 将被点击的page插入到active的最后
      newActive.push(activeItem);
      return {
        ...state,
        active: newActive,
      };
    },
  },
};
