const start = `<script  setup>`;
const end = `</script>`;
let variablesData = ``;
let functionData = ``;
const scriptCombined = () => {
  return `
${start}
import { Delete, Edit, Plus } from '@element-plus/icons-vue';
import request from '@/utils/request';
const formData = ref({});
const formDataOperation = {
  get: (key) => {
    return formData.value[key];
  },
  set: (key, value) => {
    formData.value[key] = value;
  }
};
const pageData = ref({})
${getFormVariablesData()}
${getVariablesData()}
${functionData}
${end}
`;
};

let formVariablesStructure = {};
let variablesStructure = {};


const addFormVariables = (tier, key, type) => {
  let obj = formVariablesStructure;
  if (tier.length > 0) {
    tier.forEach((t) => {
      obj = obj[t];
    });
    obj[key] = type;
  } else {
    obj[key] = type;
  }
};
const addVariables = (key, type) => {
  variablesStructure[key] = type;
};
const getFormVariablesData = () => {
  let res = ``;
  Object.keys(formVariablesStructure).forEach((t) => {
    if (t) {
      res += `
    const ${t} = ref(${JSON.stringify(formVariablesStructure[t]) || ''})
    `;
    }
  });
  return res;

  // return `
  // const pageForm = ref(${JSON.stringify(formVariablesStructure)})
  // `;
};
const getVariablesData = () => {
  let res = ``;
  Object.keys(variablesStructure).forEach((t) => {
    res += `
    const ${t} = ref(${JSON.stringify(variablesStructure[t])})
    `;
  });
  return res;
};

const addFunction = (code) => {
  functionData += code;
};
const resetScript = (data) => {
  variablesData = ``;
  variablesStructure = {};
  functionData = ``;
};

export {
  addFormVariables, addVariables, addFunction, scriptCombined, resetScript
};
