import {
  addFormVariables, addFunction
} from './script.js';

import {
  addCode
} from './template.js';

const getFormField = (tier, formData, elementData) => {
  let res = ``;
  if (tier && tier.length > 0) {
    tier.forEach((item, index) => {
      if (index === 0) {
        res += `${item}`;
      } else {
        res += `['${item}']`;
      }
    });
  }
  // if (formData !== '') {
  //   res += `['${formData}']`;
  // }
  if (elementData !== '') {
    res += `['${elementData}']`;
  }
  return res;
};

const getInputCode = (data) => {
  let code = ``;
  let variablesCode = ``;
  let functionCode = ``;
  let isForm = data.isForm || false;
  let tier = data.tier || [];
  let inputData = data.elementData || {};
  let formData = data.formData || {};
  if (isForm) {
    code += `<el-form-item label="${inputData.options.name || ''}" :required="${inputData.options.required}">`;
  } else {
    code += `<div> <span>${inputData.options.name}</span>`;
  }
  addFormVariables(tier, inputData.options.key || ('input-' + inputData.options.uuid), '');
  code += `<el-input v-model="${getFormField(tier, formData.options ? (formData.options.key || ('form' + formData.options.uuid)) : '', inputData.options.key || ('input-' + inputData.options.uuid))}" ${inputData.options.disabled ? 'disabled' : ''}
              type="${inputData.options.type}" ${inputData.options.type === 'password' ? 'show-password' : ''} autocomplete="new-password" />`;
  if (isForm) {
    code += `</el-form-item>`;
  } else {
    code += `</div>`;
  }
  addCode(code);

};

export {
  getInputCode
};
