// 组件删除
import { request } from 'utils/fetch';
import { api } from 'constants/config';
import { actionTypes } from 'constants/action-types';
import { commentDeltedState } from './commentDelted.types';
import { selectLayers } from './create-app';
import message from 'components/message';
export function getRecycleBin() {
  // 获取回收站
  return async (dispatch, getState) => {
    const { createApp } = getState();
    const { id: screenId } = createApp;
    const result: {
      // eslint-disable-next-line @typescript-eslint/ban-types
      recycleItems: object;
    } = await request(api.componentsGetRecycle, undefined, { screenId });
    if (result) {
      dispatch({
        type: actionTypes.RECYCLE_DATA,
        payload: result.recycleItems
      });
    }
    return result;
  };
}

// 清空回收站
export function delRecycleBin(recycleItems: number[]) {
  return async (dispatch, getState) => {
    const { createApp } = getState();
    const { id: screenId } = createApp;
    const result: {
      // eslint-disable-next-line @typescript-eslint/ban-types
      recycleItems: object;
    } = await request(
      api.componentsDelete,
      undefined,
      undefined,
      JSON.stringify({
        recycleItems,
        screenId: screenId
      })
    );
    if (result) {
      message.success('清空操作完成');
      dispatch({
        type: actionTypes.RECYCLE_DATA,
        payload: result.recycleItems
      });
      return result;
    } else {
      message.error('操作失败，请重试');
    }
  };
}

export function resetRecycleBin(recycleItems: number[]) {
  return async (dispatch, getState) => {
    const { createApp } = getState();
    const { id: screenId } = createApp;
    const result: {
      recycleItems: object;
      layers: object;
      components: object[];
    } = await request(
      api.componentsRecover,
      undefined,
      undefined,
      JSON.stringify({
        recycleItems,
        screenId: screenId
      })
    );
    if (result) {
      message.success('组件恢复成功，在图层最上方');

      dispatch({
        type: actionTypes.RECYCLE_DATA,
        payload: result.recycleItems
      });

      dispatch(selectLayers(result.layers[0].id));

      dispatch({
        type: actionTypes.LAYER_UPDATE,
        payload: {
          id: screenId,
          layers: result.layers
        }
      });

      return { ...result, components: result.components.map((item) => ({ ...item, screenId })) }; // 注意这里恢复的时候，服务端是没有返回组件所属的大屏id的，导致componentsById更新时该组件没有screenId
    } else {
      message.error('恢复失败，请重试');
    }
  };
}

export function addRecycleBin(recycleItems: any[]) {
  // 添加到回收站
  return async (dispatch) => {
    dispatch({
      type: actionTypes.RECYCLE_DATA,
      payload: recycleItems
    });
  };
}

const initialState: commentDeltedState = [];

export default function reducer(state = initialState, { type, payload }): commentDeltedState {
  switch (type) {
    case actionTypes.RECYCLE_DATA: {
      return [...payload];
    }
    default:
      return [...state];
  }
}
