import { ref } from 'vue';
import { ChartEnum } from '@/enums/pageEnum';
import { fetchPathByName, routerTurnByPath, openNewWindow, previewPath } from '@/utils';
import { Chartype } from '../../../index.d';

export const useModalDataInit = () => {
  const modalShow = ref<boolean>(false);
  const modalData = ref<Chartype | null>(null);

  // 关闭 modal
  const closeModal = () => {
    modalShow.value = false;
    modalData.value = null;
  };

  // 缩放处理
  const resizeHandle = (cardData: Chartype) => {
    if (!cardData) return;
    modalShow.value = true;
    modalData.value = cardData;
  };

  // 编辑处理
  const editHandle = (cardData: Chartype) => {
    if (!cardData) return;
    let path = fetchPathByName(ChartEnum.CHART_HOME_NAME, 'href');
    path = path.replace('#','')
    routerTurnByPath(path, [cardData.id], undefined, false);
  };

  // 预览处理
  const previewHandle = (cardData: Chartype) => {
    openNewWindow(previewPath(cardData.id));
  };

  return {
    modalData,
    modalShow,
    closeModal,
    resizeHandle,
    editHandle,
    previewHandle,
  };
};
