import { defineComponent as b, mergeModels as k, useModel as $, ref as y, computed as M, watch as h, createBlock as V, openBlock as D, unref as F, mergeProps as j, createSlots as w, renderList as x, withCtx as N, resolveDynamicComponent as z } from "vue";
import { r as X, ad as H, j as K } from "./start-CE9SawgU.js";
import "vue-i18n";
import "pinia";
import "./header-Bn1EKnBE.js";
import P from "./SettingInput-DIJQqVYx.js";
import { _ as J } from "./SettingCheckbox.vue_vue_type_script_setup_true_lang-BLtIa1u3.js";
import { _ as U } from "./SettingCombobox.vue_vue_type_script_setup_true_lang-B_kLa2J_.js";
import { _ as q } from "./SettingDatePicker.vue_vue_type_script_setup_true_lang-V_-FVGjr.js";
var B = /* @__PURE__ */ ((e) => (e.INPUT = "input", e.CHECKBOX = "checkbox", e.DATE_PICKER = "actindo-date-picker", e.COMBOBOX = "actindo-combobox", e))(B || {});
const Q = {
  input: {
    component: P
    // renderComponent: "SettingInput",
    // renderImportPath:
    //   "/Actindo/Modules/Actindo/Setting/Resources/vue3/external-components/SettingInput.mjs",
  },
  checkbox: {
    component: J
    // renderComponent: "SettingCheckbox",
    // renderImportPath:
    //   "/Actindo/Modules/Actindo/Setting/Resources/vue3/external-components/SettingCheckbox.mjs",
  },
  "actindo-combobox": {
    component: U
    // renderComponent: "SettingCombobox",
    // renderImportPath:
    //   "/Actindo/Modules/Actindo/Setting/Resources/vue3/external-components/SettingCombobox.mjs",
  },
  "actindo-date-picker": {
    component: q
    // renderComponent: "SettingDatePicker",
    // renderImportPath:
    //   "/Actindo/Modules/Actindo/Setting/Resources/vue3/external-components/SettingDatePicker.mjs",
  }
};
function W(e, o, t, n) {
  return e <= t && t <= o || e <= n && n <= o ? !0 : t < e && o < n;
}
function G(e) {
  for (let o = 0; o < e.length - 1; o++)
    for (let t = o + 1; t < e.length; t++)
      if (t in e && W(
        e[o].valid_from,
        e[o].valid_to,
        e[t].valid_from,
        e[t].valid_to
      ))
        return `Dates overlap! Rows: ${e[o].index} and ${e[t].index}; Vat codes: ${e[o].nintendo_vat_code} and ${e[t].nintendo_vat_code}`;
  return "";
}
function S(e) {
  const o = [];
  let t = "";
  return e.forEach(
    ({
      tax_zone_match_criterion: n,
      valid_from: p,
      valid_to: s,
      nintendo_vat_code: l
    }, v) => {
      const m = n;
      o[m] || (o[m] = []), o[m].push({
        index: v + 1,
        valid_from: new Date(p).getTime(),
        valid_to: new Date(s).getTime(),
        nintendo_vat_code: l
      });
    }
  ), o.forEach((n) => {
    n.length && (t = t || G(n));
  }), t;
}
const E = {
  validateTaxZoneMatchCriterionRows: S
}, O = (e) => {
  const [o, t, ...n] = e.split(".");
  return [t, o, ...n].join("/");
}, A = (e) => /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(e), I = ([e, o, t], n, p) => {
  let s = n[e], l = n[o];
  if (!s || !l)
    return;
  if (X.locale() == "de" && (s = O(s), l = O(l)), !A(s) || !A(l))
    return "date format is incorrect, the date must be seperated by '/' e.g. '1/1/2020'";
  const m = Date.parse(s) + t + Date.parse(l);
  return !Function("return " + m)() && `Row ${p + 1} for data ${JSON.stringify(n)} broke condition "${n[e]}" ${t} "${n[o]}"`;
}, L = /* @__PURE__ */ b({
  __name: "SettingMultiBuilder",
  props: /* @__PURE__ */ k({
    modelValue: {},
    data: {},
    options: {},
    settings: {},
    loading: { type: Boolean },
    readonly: { type: Boolean },
    isDefault: { type: Boolean }
  }, {
    modelValue: { default: () => [] },
    modelModifiers: {},
    valid: { type: Boolean, default: !0 },
    validModifiers: {}
  }),
  emits: ["update:modelValue", "update:valid"],
  setup(e) {
    const o = e, t = $(e, "modelValue"), n = $(e, "valid"), p = y({}), s = y(""), l = y([]), v = M(() => ({
      data: t.value || [],
      minRowCount: 1,
      hideGlobalSearch: !0,
      canReorderRows: !1,
      createActions: !0,
      columns: l.value
    }));
    function m() {
      const a = {};
      Array.isArray(t.value) ? t.value.push(a) : t.value = [a];
    }
    function g({ rowIndex: a }) {
      if (!Array.isArray(t.value)) {
        t.value = [];
        return;
      }
      t.value = t.value.filter((i, c) => c !== a);
    }
    const _ = ({ id: a }, i) => `${a}-${i}`;
    return h(
      () => o.options?.options,
      (a) => {
        l.value = [], Array.isArray(a) && (l.value = a.map((i, c) => {
          i.type === B.COMBOBOX && i.loadItems && i.remote && i.displayField && i.valueField && H(i.remote).then(({ data: f }) => {
            l.value[c].properties.items = f.map((C) => ({
              [i.valueField]: C[i.valueField],
              [i.displayField]: C[i.displayField]
            }));
          });
          const r = Q[i.type], { properties: u, vueComponents: d } = "component" in r ? {
            properties: {
              ...i,
              component: r.component
            },
            vueComponents: void 0
          } : {
            properties: i,
            vueComponents: { ...r }
          };
          return {
            id: `id${c}`,
            header: "",
            properties: u,
            vueComponents: d
          };
        }));
      },
      { immediate: !0 }
    ), h(
      [p, s],
      ([a, i]) => {
        n.value = !i && Object.values(a).every(Boolean);
      },
      { deep: !0, immediate: !0 }
    ), h(
      t,
      (a) => {
        const i = [];
        try {
          if (o.options?.validationRules || !Array.isArray(a))
            return;
          const { rowValidation: c, customValidation: r } = o.options.validationRules;
          a.forEach(
            (u, d) => c.forEach(
              (f) => i.push(I(f, u, d))
            )
          ), r.forEach((u) => {
            const d = E[u];
            if (!d)
              return console.error(
                `Custom validation function "${u}" does not exist in setting multi builder."`
              );
            i.push(d(a));
          });
        } finally {
          s.value = i.filter(Boolean).join(`
`);
        }
      },
      { deep: !0 }
    ), (a, i) => (D(), V(F(K), j(v.value, {
      onCreate: m,
      onRemove: g
    }), w({ _: 2 }, [
      x(l.value, (c) => ({
        name: `cell-${c.id}`,
        fn: N(({ cell: r, row: u, rowIndex: d }) => [
          (D(), V(z(r.properties.component ?? r.CellRenderer), {
            modelValue: u[r.properties.serializationName],
            "onUpdate:modelValue": (f) => u[r.properties.serializationName] = f,
            valid: p.value[_(r, d)],
            options: r.properties,
            readonly: !r.properties.serializationName,
            "onUpdate:valid": (f) => p.value[_(r, d)] = f
          }, null, 40, ["modelValue", "onUpdate:modelValue", "valid", "options", "readonly", "onUpdate:valid"]))
        ])
      }))
    ]), 1040));
  }
});
function R(e) {
  const o = e;
  o.__i18n = o.__i18n || [], o.__i18n.push({
    locale: "",
    resource: {
      "en-US": {
        "setting-plugin": {
          "setting-input": {
            regex: { t: 0, b: { t: 2, i: [{ t: 3, v: "Has to match " }, { t: 4, k: "pattern" }] } }
          }
        }
      },
      "de-DE": {
        "setting-plugin": {
          "setting-input": {
            regex: { t: 0, b: { t: 2, i: [{ t: 3, v: "Muss mit " }, { t: 4, k: "pattern" }, { t: 3, v: " übereinstimmen" }] } }
          }
        }
      }
    }
  });
}
typeof R == "function" && R(L);
export {
  L as default
};
