import { defineComponent as N, mergeModels as M, useModel as H, ref as A, createBlock as V, openBlock as m, unref as o, mergeProps as W, createSlots as Y, withCtx as h, createVNode as _, computed as g, renderList as $, watch as O, createElementVNode as F, createElementBlock as I, Fragment as U, createCommentVNode as B, withDirectives as se, renderSlot as oe, createTextVNode as re, toDisplayString as de, vShow as ve } from "vue";
import { useI18n as R } from "vue-i18n";
import { a1 as me, a2 as ce, j as ee, I as G, d as pe, X as fe, a3 as be, a4 as ye, T as Q, a5 as X, a6 as ge, a7 as Ve, a8 as he, s as z, a9 as q, A as Se, a as Ae, aa as ke, o as _e } from "./start-CE9SawgU.js";
import "pinia";
import { M as Te } from "./header-Bn1EKnBE.js";
import { _ as ae, e as P } from "./DatahubDataTypeEditor.vue_vue_type_script_setup_true_lang-Blx83HK-.js";
import { b as Ce, g as we, a as Ie } from "./utils-BfOsse_-.js";
import { a as Ee } from "./constants-CwfsvyFM.js";
const De = {
  rowSelection: ce.NONE,
  hideGlobalSearch: !0,
  hideFilters: !0,
  hideColumnCustomization: !1,
  hidePagination: !1,
  sortType: me.NONE
}, te = /* @__PURE__ */ N({
  __name: "VariantsList",
  props: /* @__PURE__ */ M({
    attribute: {}
  }, {
    modelValue: {},
    modelModifiers: {}
  }),
  emits: ["update:modelValue"],
  setup(c) {
    const l = H(c, "modelValue"), { t: e } = R(), b = {
      hideRemove: !1,
      hideGlobalSearch: !0,
      createActions: !0,
      minRowCount: 1,
      columns: [
        {
          id: "item",
          header: ""
        }
      ]
    }, p = A([]), r = A(1);
    function n() {
      l.value = [...l.value ?? [], { value: void 0 }];
    }
    function s({ rowIndex: t }) {
      l.value = l.value?.filter((w, T) => T !== t) ?? [];
    }
    function i() {
      l.value = p.value.map((t) => ({ value: [t] })), r.value = r.value + 1;
    }
    function f(t) {
      p.value = t?.detail?.items ?? [];
    }
    return (t, w) => (m(), V(o(ee), W({
      key: r.value,
      data: l.value
    }, b, {
      onCreate: n,
      onRemove: s
    }), Y({
      "cell-item": h(({ rowIndex: T }) => [
        _(ae, {
          modelValue: l.value[T].value,
          "onUpdate:modelValue": (k) => l.value[T].value = k,
          attribute: t.attribute,
          "data-type-instance": t.attribute.dataTypeInstance,
          label: t.attribute.name,
          onAllItems: f,
          onString: (k) => l.value[T].asString = k?.detail?.asString
        }, null, 8, ["modelValue", "onUpdate:modelValue", "attribute", "data-type-instance", "label", "onString"])
      ]),
      _: 2
    }, [
      p.value?.length ? {
        name: "actions",
        fn: h(() => [
          _(o(G), {
            variant: "text",
            color: "primary",
            text: o(e)("datahub-plugin.add-variant.addAll"),
            onClick: i
          }, null, 8, ["text"])
        ]),
        key: "0"
      } : void 0
    ]), 1040, ["data"]));
  }
});
function E(c) {
  const l = c;
  l.__i18n = l.__i18n || [], l.__i18n.push({
    locale: "",
    resource: {
      "en-US": {
        "datahub-plugin": {
          "add-variant": {
            sku: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "SKU" } },
            addAll: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Add all" } },
            summary: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Summary" } },
            adjustSku: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Adjust SKU" } }
          }
        }
      },
      "de-DE": {
        "datahub-plugin": {
          "add-variant": {
            sku: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "SKU" } },
            addAll: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Alle hinzufügen" } },
            summary: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Zusammenfassung" } },
            adjustSku: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "SKU anpassen" } }
          }
        }
      }
    }
  });
}
typeof E == "function" && E(te);
const le = /* @__PURE__ */ N({
  __name: "AdjustSkus",
  props: /* @__PURE__ */ M({
    definingAttributes: {},
    variants: { default: () => [] }
  }, {
    modelValue: { default: () => [] },
    modelModifiers: {}
  }),
  emits: ["update:modelValue"],
  setup(c) {
    const l = c, { t: e } = R(), b = H(c, "modelValue"), p = g(() => ({
      hideRemove: !1,
      hideGlobalSearch: !0,
      createActions: !1,
      minRowCount: 1,
      columns: [
        {
          id: "sku",
          header: e("datahub-plugin.add-variant.sku")
        }
      ].concat(
        l.definingAttributes.map(({ name: n, key: s }) => ({
          id: s,
          header: n
        }))
      )
    }));
    function r({ rowIndex: n }) {
      b.value = b.value.filter(
        (s, i) => i !== n
      );
    }
    return (n, s) => (m(), V(o(ee), W({ data: b.value }, p.value, { onRemove: r }), Y({
      "cell-sku": h(({ rowIndex: i }) => [
        _(o(pe), {
          modelValue: b.value[i].sku.value,
          "onUpdate:modelValue": (f) => b.value[i].sku.value = f
        }, null, 8, ["modelValue", "onUpdate:modelValue"])
      ]),
      _: 2
    }, [
      $(n.definingAttributes, (i) => ({
        name: `cell-${i.key}`,
        fn: h(({ cellValue: f }) => [
          _(ae, {
            "model-value": f.value,
            attribute: i,
            "data-type-instance": i.dataTypeInstance,
            "read-only": ""
          }, null, 8, ["model-value", "attribute", "data-type-instance"])
        ])
      }))
    ]), 1040, ["data"]));
  }
});
typeof E == "function" && E(le);
const Ue = (c, l, e, b) => {
  const p = c.replace("{L}", String(b + 1)).replace("{master}", e?.sku);
  return Object.entries(l).reduce((r, [n, { value: s, asString: i }]) => r.replace(`{${n}}`, i ?? s), p);
}, Oe = (c, l, e) => e.every((b) => l.some(({ dataType: p, key: r }) => {
  const n = `_${r}`, s = b[n], i = c[r]?.asString ?? c[r]?.value;
  switch (p) {
    case Ee:
      return Ie(s) !== i;
    case "Actindo\\Modules\\Actindo\\DataHub\\DataTypes\\BooleanValue":
      return we(s) !== i;
    case "Actindo\\Modules\\Actindo\\DataHub\\DataTypes\\UnitValue":
      return Ce(s) !== i;
    default:
      return s !== i;
  }
})), Be = { class: "flex-grow-1 overflow-hidden ma-6" }, J = 4, L = -1, ne = /* @__PURE__ */ N({
  __name: "AddVariantsModal",
  props: /* @__PURE__ */ M({
    attribute: { default: void 0 },
    definingAttributes: { default: () => [] },
    variants: { default: () => [] },
    skuFormula: { default: "{master}-{L}" },
    submitting: { type: Boolean }
  }, {
    modelValue: { type: Boolean },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ M(["save"], ["update:modelValue"]),
  setup(c, { emit: l }) {
    const e = c, b = l, p = H(c, "modelValue"), { t: r } = R(), n = A(1), s = g(() => {
      let d = 0;
      return (e.definingAttributes ?? []).map((a) => ({
        ...a,
        value: ++d
      })).concat([
        {
          value: ++d,
          name: r("datahub-plugin.add-variant.adjustSku"),
          viewType: L
        }
      ]);
    }), i = g(
      () => s.value.find(({ value: d }) => d === n.value)
    ), f = A({}), t = A([]), w = g(() => n.value === s.value[0].value), T = g(() => n.value === s.value.at(-1).value), k = g(
      () => !e.definingAttributes?.length || e.submitting
    ), j = g(() => i.value?.viewType === L ? !!t.value.length : !0);
    function D() {
      if (T.value) {
        const d = t.value.map(
          (v) => Object.fromEntries(
            Object.entries(v).map(([a, { value: S }]) => [a, S])
          )
        );
        return b("save", d);
      }
      n.value = n.value + 1;
    }
    return O(
      () => e.definingAttributes?.length,
      (d) => {
        d && (f.value = e.definingAttributes.reduce(
          (v, { key: a }) => ({
            ...v,
            [a]: []
          }),
          {}
        ));
      },
      { immediate: !0 }
    ), O(n, () => {
      i.value?.viewType === L && (t.value = Object.entries(f.value).reduce(
        (d, [v, a]) => a.flatMap(
          (S) => d.length ? d.map((K) => ({ ...K, [v]: S })) : { [v]: S }
        ),
        []
      ).filter((d) => Oe(d, e.definingAttributes, e.variants)).map((d, v) => ({
        ...d,
        sku: {
          value: Ue(
            e.skuFormula,
            d,
            e.attribute,
            e.variants.length + v
          )
        }
      })));
    }), (d, v) => (m(), V(o(fe), {
      modelValue: p.value,
      "onUpdate:modelValue": v[3] || (v[3] = (a) => p.value = a),
      "padding-bottom": "",
      "padding-x": "",
      "content-padding-y": "0",
      "max-width": "98%",
      height: "98%"
    }, {
      "left-actions": h(() => [
        _(o(G), {
          color: "secondary",
          variant: "outlined",
          text: o(r)("common.back"),
          disabled: w.value,
          onClick: v[2] || (v[2] = (a) => --n.value)
        }, null, 8, ["text", "disabled"])
      ]),
      actions: h(() => [
        _(o(G), {
          color: "primary",
          text: T.value ? o(r)("common.applyChanges") : o(r)("common.continue"),
          disabled: k.value || !j.value,
          onClick: D
        }, null, 8, ["text", "disabled"])
      ]),
      default: h(() => [
        _(o(be), {
          modelValue: n.value,
          "onUpdate:modelValue": v[1] || (v[1] = (a) => n.value = a),
          disabled: k.value,
          class: "h-100 d-flex flex-column overflow-hidden"
        }, {
          default: h(() => [
            _(o(ye), { class: "flex-shrink-0" }, {
              default: h(() => [
                k.value ? (m(), I(U, { key: 0 }, $(J, (a) => (m(), I(U, { key: a }, [
                  _(o(Q), {
                    type: "avatar",
                    height: "80"
                  }),
                  a < J ? (m(), V(o(X), { key: 0 })) : B("", !0)
                ], 64))), 64)) : (m(!0), I(U, { key: 1 }, $(s.value, (a, S) => (m(), I(U, {
                  key: a.value
                }, [
                  _(o(ge), {
                    value: a.value,
                    title: a.name
                  }, null, 8, ["value", "title"]),
                  S < s.value.length - 1 ? (m(), V(o(X), { key: 0 })) : B("", !0)
                ], 64))), 128))
              ]),
              _: 1
            }),
            F("div", Be, [
              k.value ? (m(), V(o(Q), {
                key: 0,
                type: "table-thead, table-tbody"
              })) : (m(), V(o(Ve), { key: 1 }, {
                default: h(() => [
                  (m(!0), I(U, null, $(s.value, (a) => (m(), V(o(he), {
                    key: a.value,
                    value: a.value
                  }, {
                    default: h(() => [
                      a.viewType === L ? (m(), V(le, {
                        key: 0,
                        modelValue: t.value,
                        "onUpdate:modelValue": v[0] || (v[0] = (S) => t.value = S),
                        "defining-attributes": d.definingAttributes,
                        variants: d.variants
                      }, null, 8, ["modelValue", "defining-attributes", "variants"])) : a.dataTypeInstance && f.value[a.key] ? (m(), V(te, {
                        key: 1,
                        modelValue: f.value[a.key],
                        "onUpdate:modelValue": (S) => f.value[a.key] = S,
                        attribute: a
                      }, null, 8, ["modelValue", "onUpdate:modelValue", "attribute"])) : B("", !0)
                    ]),
                    _: 2
                  }, 1032, ["value"]))), 128))
                ]),
                _: 1
              }))
            ])
          ]),
          _: 1
        }, 8, ["modelValue", "disabled"])
      ]),
      _: 1
    }, 8, ["modelValue"]));
  }
});
typeof E == "function" && E(ne);
const Le = { class: "variant-value" }, $e = { class: "mx-4 my-4" }, Me = { class: "flex-grow-1" }, ie = /* @__PURE__ */ N({
  __name: "DatahubDataTypeVariantValueEditor",
  props: {
    attributeSetId: {},
    variantSetController: {},
    variantController: {},
    additionalColumns: { default: () => [] },
    addVariants: {},
    modelValue: {},
    readOnly: { type: Boolean },
    attribute: {},
    massEditing: { type: Boolean },
    label: {},
    fieldName: {},
    dataTypeInstance: {},
    loading: { type: Boolean },
    succeeded: { type: Boolean }
  },
  emits: ["validated", "update:model-value", "initialized", "clicked-row"],
  setup(c, { emit: l }) {
    const e = c, b = l, { t: p } = R(), r = A(), n = A(!0), s = A(!1), i = A([]), f = A(!1), t = A(
      e.modelValue ? { ...e.modelValue } : {
        childrenIds: {},
        toCreate: []
      }
    ), w = g(() => !t.value?.variantSetId && !i.value?.length ? null : i.value.find(
      ({ id: u }) => u === t.value?.variantSetId
    )), T = g(
      () => w.value?.definingAttributes ?? []
    ), k = g(() => t.value?.isMasterEntity === !1), j = g(
      () => r.value?.table?.rows.value?.length > 0 || r.value?.table?.isLoading.value
    ), D = A([]), d = g(
      () => [
        { id: "id", header: "common.id", isVisible: !1 },
        {
          id: "toCreate",
          header: "datahub-ui.dh-table.status",
          accessor: ({ id: u }) => p(u ? "datahub-ui.dh-table.createdStatus" : "datahub-ui.dh-table.toBeCreated")
        },
        ...e.additionalColumns,
        ...D.value
      ]
    ), v = g(() => ({
      ids: t.value?.childrenIds ? Object.values(t.value.childrenIds) : []
    }));
    async function a(u) {
      return z.post(
        P(e.variantController, "listChildren"),
        u
      );
    }
    const S = g(
      () => ({
        data: a,
        columns: d.value,
        customQuery: v.value,
        freeze: !t.value?.variantSetId,
        isLoading: e.loading,
        createActions: !e.readOnly && !!t.value?.variantSetId,
        readonly: e.readOnly || e.attribute?.configuration?.readOnly,
        ...De
      })
    );
    O(t, (u) => b("update:model-value", u), { deep: !0 }), O(
      () => e.attributeSetId,
      async (u) => {
        if (i.value = [], n.value = !1, !!u)
          try {
            n.value = !0;
            const { data: y } = await z.post(
              P(e.variantSetController, "getList"),
              { attributeSetId: u, start: 0, limit: Te }
            );
            i.value = y;
          } catch (y) {
            console.error(y);
          } finally {
            n.value = !1;
          }
      },
      { immediate: !0 }
    ), O(
      () => t.value?.variantSetId,
      async (u) => {
        if (D.value = [], !!u)
          try {
            const {
              definingAttributesColumnDefinition: y,
              changeableAttributesColumnDefinition: C
            } = await z.post(
              P(
                e.variantController,
                "getColumnConfigurationsAndVariantSet"
              ),
              { variantSetId: u }
            );
            D.value = [
              ...y.map(
                (x) => q({ ...x, hidden: !1 })
              ),
              ...C.map(
                (x) => q({ ...x, hidden: !0 })
              )
            ];
          } catch (y) {
            console.error(y);
          }
      },
      { immediate: !0 }
    );
    async function K(u) {
      if (e.addVariants)
        try {
          f.value = !0, await e.addVariants({
            variants: u,
            variantSetId: t.value.variantSetId
          }) && (s.value = !1);
        } catch {
        } finally {
          f.value = !1;
        }
    }
    function ue() {
      f.value = !1, s.value = !0;
    }
    return (u, y) => (m(), I("div", Le, [
      F("div", $e, [
        !u.massEditing && u.label ? (m(), V(o(Se), {
          key: 0,
          label: u.label,
          disabled: u.readOnly,
          variant: "plain",
          class: "mb-8"
        }, null, 8, ["label", "disabled"])) : B("", !0),
        k.value ? oe(u.$slots, "child-redirect", { key: 1 }, void 0, !0) : (m(), V(o(Ae), {
          key: 2,
          modelValue: t.value.variantSetId,
          "onUpdate:modelValue": y[0] || (y[0] = (C) => t.value.variantSetId = C),
          items: i.value,
          readonly: u.readOnly || u.attribute?.configuration?.readOnly,
          label: o(p)("datahub-plugin.variant-value.variantSet"),
          loading: n.value || u.loading,
          disabled: j.value,
          "item-title": "name",
          "item-value": "id",
          "max-width": "567",
          "page-size": "",
          search: ""
        }, null, 8, ["modelValue", "items", "readonly", "label", "loading", "disabled"]))
      ]),
      se(F("div", Me, [
        _(o(ke), W({
          ref_key: "table",
          ref: r,
          embedded: ""
        }, S.value, {
          onClickedRow: y[2] || (y[2] = (C) => u.$emit("clicked-row", C)),
          onCreate: ue
        }), Y({
          modals: h(() => [
            s.value ? (m(), V(ne, {
              key: 0,
              modelValue: s.value,
              "onUpdate:modelValue": y[1] || (y[1] = (C) => s.value = C),
              submitting: f.value,
              attribute: u.attribute,
              "defining-attributes": T.value,
              variants: r.value?.table?.rows.value,
              "sku-formula": w.value?.formulas?.sku,
              onSave: K
            }, null, 8, ["modelValue", "submitting", "attribute", "defining-attributes", "variants", "sku-formula"])) : B("", !0)
          ]),
          _: 2
        }, [
          t.value?.variantSetId ? void 0 : {
            name: "no-data-comment",
            fn: h(() => [
              re(de(o(p)("datahub-plugin.variant-value.emptyTable")), 1)
            ]),
            key: "0"
          }
        ]), 1040)
      ], 512), [
        [ve, !k.value]
      ])
    ]));
  }
});
function Z(c) {
  const l = c;
  l.__i18n = l.__i18n || [], l.__i18n.push({
    locale: "",
    resource: {
      "en-US": {
        "datahub-plugin": {
          "variant-value": {
            emptyTable: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Select variant sets" } },
            variantSet: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Variant set" } }
          }
        }
      },
      "de-DE": {
        "datahub-plugin": {
          "variant-value": {
            emptyTable: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Variantensätze auswählen" } },
            variantSet: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Variantensatz" } }
          }
        }
      }
    }
  });
}
typeof Z == "function" && Z(ie);
const Ge = /* @__PURE__ */ _e(ie, [["__scopeId", "data-v-48e55caa"]]);
export {
  Ge as default
};
