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