import { defineComponent as C, computed as b, resolveComponent as N, openBlock as h, createElementBlock as v, createVNode as S, toDisplayString as F, unref as w, h as d } from "vue";
import { t as x, u as B } from "./app-5956b5d6.js";
import { components as c } from "@feedmepos/ui-library";
import { c as O } from "./object-27ce045b.js";
import "pinia";
import "vue-router";
const U = { key: 0 }, z = {
  key: 1,
  class: "text-fm-color-neutral-gray-500"
}, L = /* @__PURE__ */ C({
  __name: "CustomAttributesForm",
  props: {
    entity: {},
    modelValue: {},
    valueOptions: {}
  },
  emits: ["update:modelValue"],
  setup(g, { emit: k }) {
    const s = g, V = k, { t: u } = x(), f = B();
    function _(t) {
      var e;
      if ((e = s.valueOptions) != null && e[t] && s.valueOptions[t].length > 0)
        return s.valueOptions[t];
      if (s.entity === "restaurant") {
        const a = f.restaurants.value.map((i) => {
          var o;
          return (o = i.customAttributes) == null ? void 0 : o[t];
        }).filter((i) => typeof i == "string" && !!i), n = [...new Set(a)];
        return n.length > 0 ? n : void 0;
      }
    }
    const y = b(() => {
      var t, e;
      return ((e = (t = f.currentBusiness.value) == null ? void 0 : t.attributeSettings) == null ? void 0 : e.filter(
        (a) => a.entity === s.entity
      )) || [];
    }), p = b(() => y.value.map((t) => {
      var n;
      const e = _(t.key), a = t.type === "boolean" ? !1 : "";
      return {
        key: t.key,
        type: t.type,
        value: ((n = s.modelValue) == null ? void 0 : n[t.key]) ?? a,
        options: e,
        readonly: t.readonly === !0
      };
    }) || []), A = [
      {
        header: () => u("portalshell.customAttributes.form.key"),
        accessorKey: "key",
        enableSorting: !1
      },
      {
        header: () => u("portalshell.customAttributes.form.value"),
        accessorKey: "value",
        enableSorting: !1,
        cell: (t) => {
          var i;
          const e = t.row.original, a = (o) => {
            const l = O(s.modelValue), r = l[e.key] = typeof o == "string" ? o.trim() : o;
            if (e.type === "number")
              if (r === "" || r === null || r === void 0)
                l[e.key] = "";
              else {
                if (typeof r == "boolean" || isNaN(Number(r)))
                  return;
                l[e.key] = Number(r);
              }
            if (e.type === "boolean")
              if (r === "" || r === null || r === void 0)
                l[e.key] = "";
              else if (typeof r == "string") {
                const m = r.toLowerCase();
                if (m === "true")
                  l[e.key] = !0;
                else if (m === "false")
                  l[e.key] = !1;
                else
                  return;
              } else if (typeof r == "boolean")
                l[e.key] = r;
              else
                return;
            Object.keys(l).forEach((m) => {
              (l[m] === "" || l[m] === null || l[m] === void 0) && delete l[m];
            }), V("update:modelValue", l);
          }, n = ((i = y.value.find((o) => o.key === e.key)) == null ? void 0 : i.options) || [];
          return n != null && n.length ? d(c.FmSelect, {
            modelValue: e.value,
            placeholder: u("portalshell.customAttributes.form.valuePlaceholder"),
            items: n.map((o) => ({ label: o, value: o })),
            disabled: e.readonly,
            "onUpdate:modelValue": a
          }) : e.type === "boolean" ? d(c.FmCheckbox, {
            modelValue: e.value,
            value: !0,
            disabled: e.readonly,
            "onUpdate:modelValue": a
          }) : e.type === "date" ? d(c.FmDatePicker, {
            modelValue: e.value,
            placeholder: u("portalshell.customAttributes.form.valuePlaceholder"),
            disabled: e.readonly,
            "onUpdate:modelValue": a
          }) : d(c.FmTextField, {
            modelValue: e.value,
            placeholder: u("portalshell.customAttributes.form.valuePlaceholder"),
            datalist: e.options,
            disabled: e.readonly,
            rules: [
              (o) => e.type === "number" && isNaN(Number(o)) ? u("portalshell.customAttributes.form.attributeValueMustBeNumber") : !0
            ],
            "onUpdate:modelValue": a
          });
        }
      }
    ];
    return (t, e) => {
      const a = N("FmTable");
      return p.value.length > 0 ? (h(), v("div", U, [
        S(a, {
          "column-defs": A,
          "row-data": p.value,
          "shrink-at": !1,
          "page-size": p.value.length,
          "hide-footer": ""
        }, null, 8, ["row-data", "page-size"])
      ])) : (h(), v("p", z, F(w(u)("portalshell.customAttributes.form.noCustomAttributes")), 1));
    };
  }
});
export {
  L as default
};
