import { getFormDesignData } from '@/api/platform/form';

export async function useGetFormDesignData(templateId: string, versionId?: string) {
  try {
    const res = await getFormDesignData(templateId, versionId);
    if (res.code === 200) {
      // const { TemplateData, LayoutData } = JSON.parse(res.data);
      return res.data;
    }
    return {};
  } catch (e) {
    console.log('error', e);
    return {};
  }
}

export function getToolbox(allTable, tools: any) {
  const retObj = {
    toolbox: {},
    control: {
      order: [],
    },
  };
  if (allTable && allTable.length > 0) {
    allTable.forEach((item, index) => {
      const table = {
        master: item.Master,
        text: item.ObjTypeText || '无数据',
        expand: false,
        children: [],
      };
      const { PropertyControlData: _controlData, ...extraTableInfo } = item;
      const PropertyControlData = JSON.parse(item.PropertyControlData);
      table.children = PropertyControlData.map(col => {
        const { DisposeType: type, InfoType: value, Text: label } = col;
        const originCompopnent = tools[type];
        if (!originCompopnent) {
          return;
        }
        const newTool = Object.create(originCompopnent);
        newTool.title = label;
        newTool.options = {
          tag: {
            ...extraTableInfo,
            ...col,
          },
        };
        let compName = `${item.ObjTypeName}${value}`;
        if (type === 'LabelValue') {
          compName = `Label${compName}`;
          newTool.options.value = label;
        }
        retObj.toolbox[compName] = newTool;
        return compName;
      }).filter(value => value);
      retObj.control.order.push(table);
    });
  }
  retObj.control.order.push({
    text: '基础元件',
    expand: false,
    children: ['label', 'link', 'datasetindex', 'textbox', 'textarea'],
  });
  return retObj;
}
