import {
  escapeObjectForInlineBinding,
  processDefaultTemplate,
  useDocumentTemplate
} from "./template.js";
import { cloneDeep, isString, isArray } from "lodash-es";
export function autoActionHeader(tableHeader) {
  let autoTableHeader = cloneDeep(tableHeader);
  if (!autoTableHeader.some((h) => h.key === "action")) autoTableHeader.push({ title: "Action", key: "action", width: "100px" });
  return autoTableHeader;
}
export function templateToHeader(template) {
  if (isString(template)) {
    try {
      template = JSON.parse(template);
    } catch (e) {
      void e;
    }
  }
  if (!isArray(template)) return [];
  return template.map((item) => {
    return {
      title: item.inputLabel || item.label || item.title || item.variableName || item.key,
      key: item.variableName || item.key,
      width: item.width && item.width > 12 ? guessWidth(item.width) : void 0
    };
  });
}
function guessWidth(width) {
  if (width == 0) return void 0;
  if (width >= 1 && width < 12) return (width * 100 / 12).toFixed(2) + "%";
  if (width > 12 && width < 100) return width + "%";
  else return width + "px";
}
export function processTemplateFormTable(item, parentTemplates, dataVariable) {
  let tableOptions = Object.assign({ title: item.inputLabel || "", formTemplate: "" }, item.inputOptions);
  let tableHeader = tableOptions.headers || [];
  let tableItemTemplate = "";
  tableHeader.forEach((h) => {
    if (h.template) {
      tableItemTemplate += `\r
<template #item.${h.key}="props">${h.template}</template>`;
    } else {
      tableItemTemplate += `\r
<template #item.${h.key}="props">{{autoSanitizedDisplay(props.item.${h.key})}}</template>`;
    }
  });
  if (!tableHeader.some((h) => h.key === "action")) tableHeader.push({ title: "Action", key: "action", width: "100px" });
  return processDefaultTemplate(item, `<template #form="{data,rules}">${useDocumentTemplate(tableOptions.formTemplate)}</template>${tableItemTemplate}`, `title="${tableOptions.title}" :headers='${escapeObjectForInlineBinding(tableHeader)}'`, void 0, dataVariable);
}
