import { defineComponent as O, mergeModels as B, useModel as L, ref as w, computed as k, watch as H, createBlock as U, openBlock as T, unref as l, withCtx as n, createElementVNode as K, createVNode as g, createCommentVNode as Z, toDisplayString as d, mergeProps as x, createTextVNode as b } from "vue";
import { useI18n as G } from "vue-i18n";
import { I as j, b as y, i as I, E as F } from "./start-BYfL32Oe.mjs";
import "pinia";
import "vue-router";
import "./header-Droxy2Kd.mjs";
import { h as q } from "./moment-h96o7c8I.mjs";
import { c as J } from "./index-fFOslvm5.mjs";
const D = () => ({
  basePrice: null,
  msrp: null,
  bulkPrices: []
}), _ = () => ({ fromQuantity: null, price: null }), V = (m) => typeof m == "string" ? Number(m.replace(",", ".")) : m, $ = (m, o) => {
  if (!m)
    return null;
  const u = q(m, "l");
  return u.set(
    o ? { hour: 23, minute: 59, second: 59 } : { hour: 0, minute: 0, second: 0 }
  ), u.unix();
}, C = {
  minRowCount: 0,
  hidePagination: !0,
  canReorderHeaders: !1,
  hideGlobalSearch: !0,
  hideNoData: !0,
  hideRemove: !1,
  columns: [
    {
      id: "basePrice",
      header: "datahub-plugin.simplePricing.basePrice"
    },
    {
      id: "price",
      header: "datahub-plugin.simplePricing.currentPricingRange"
    },
    {
      id: "bulkPrices",
      header: "datahub-plugin.simplePricing.numberScalePrices"
    },
    {
      id: "actions",
      header: "",
      width: 40,
      minWidth: 40,
      cellProps: { style: { padding: 0 } }
    }
  ]
}, A = {
  columns: [
    {
      id: "price",
      header: "datahub-plugin.simplePricing.price"
    },
    {
      id: "fromQuantity",
      header: "datahub-plugin.simplePricing.fromQuantity"
    },
    {
      id: "fromDate",
      header: "datahub-plugin.simplePricing.fromDate"
    },
    {
      id: "toDate",
      header: "datahub-plugin.simplePricing.toDate"
    }
  ]
}, W = /* @__PURE__ */ O({
  __name: "EditPricingModal",
  props: /* @__PURE__ */ B({
    config: {},
    currency: {},
    value: {},
    readonly: { type: Boolean }
  }, {
    modelValue: { type: Boolean },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ B(["save"], ["update:modelValue"]),
  setup(m, { emit: o }) {
    const u = m, v = o, P = L(m, "modelValue"), { t: s } = G(), i = w(D()), h = k(() => {
      var e;
      return !!(p(i.value.basePrice) || (e = u.config) != null && e.msrp && p(i.value.msrp) || i.value.bulkPrices.some(
        ({ price: t, fromQuantity: r }) => p(t) || p(r)
      ));
    }), M = k(() => {
      var e;
      return {
        ...C,
        data: i.value.bulkPrices,
        createActions: !u.readonly,
        columns: (e = u.config) != null && e.promotions ? A.columns : A.columns.slice(0, 2)
      };
    }), p = (e) => !e || isNaN(Number(e)) ? s("datahub-plugin.simplePricing.invalidNumber") : void 0;
    function S() {
      i.value.bulkPrices ? i.value.bulkPrices.push(_()) : i.value.bulkPrices = [_()];
    }
    function E({ rowIndex: e }) {
      confirm(s("datahub-plugin.simplePricing.confirmDeleteBulkPrice")) && i.value.bulkPrices.splice(e, 1);
    }
    function c() {
      var t;
      const e = {
        basePrice: V(i.value.basePrice),
        bulkPrices: i.value.bulkPrices.map(
          ({ fromQuantity: r, price: a, fromDate: f, toDate: z }) => {
            var Q;
            const R = {
              fromQuantity: V(r),
              price: V(a)
            };
            return (Q = u.config) != null && Q.promotions && (R.fromDate = $(f, !1), R.toDate = $(z, !0)), R;
          }
        )
      };
      (t = u.config) != null && t.msrp && (e.msrp = V(i.value.msrp)), P.value = !1, v("save", e);
    }
    return H(
      () => u.value,
      (e) => {
        i.value = J(e);
      },
      { immediate: !0 }
    ), (e, t) => (T(), U(l(j), {
      modelValue: P.value,
      "onUpdate:modelValue": t[2] || (t[2] = (r) => P.value = r),
      title: l(s)("datahub-plugin.simplePricing.edit"),
      "max-width": "600px"
    }, {
      actions: n(() => [
        g(l(F), {
          variant: "elevated",
          text: e.$t("common.saveChanges"),
          disabled: h.value,
          onClick: c
        }, null, 8, ["text", "disabled"])
      ]),
      default: n(() => {
        var r;
        return [
          K("div", null, d(l(s)("datahub-plugin.simplePricing.basePriceCalculation")), 1),
          g(l(y), {
            modelValue: i.value.basePrice,
            "onUpdate:modelValue": t[0] || (t[0] = (a) => i.value.basePrice = a),
            modelModifiers: { number: !0 },
            label: l(s)("datahub-plugin.simplePricing.basePrice"),
            "error-messages": p(i.value.basePrice),
            type: "number"
          }, null, 8, ["modelValue", "label", "error-messages"]),
          (r = e.config) != null && r.msrp ? (T(), U(l(y), {
            key: 0,
            modelValue: i.value.msrp,
            "onUpdate:modelValue": t[1] || (t[1] = (a) => i.value.msrp = a),
            modelModifiers: { number: !0 },
            label: l(s)("datahub-plugin.simplePricing.msrp"),
            "error-messages": p(i.value.msrp),
            type: "number"
          }, null, 8, ["modelValue", "label", "error-messages"])) : Z("", !0),
          g(l(I), x(M.value, {
            onCreate: S,
            onRemove: E
          }), {
            "header-price": n(({ column: a }) => [
              b(d(l(s)(a.header)), 1)
            ]),
            "header-fromQuantity": n(({ column: a }) => [
              b(d(l(s)(a.header)), 1)
            ]),
            "header-fromDate": n(({ column: a }) => [
              b(d(l(s)(a.header)), 1)
            ]),
            "header-toDate": n(({ column: a }) => [
              b(d(l(s)(a.header)), 1)
            ]),
            "cell-price": n(({ rowIndex: a }) => [
              g(l(y), {
                modelValue: i.value.bulkPrices[a].price,
                "onUpdate:modelValue": (f) => i.value.bulkPrices[a].price = f,
                modelModifiers: { number: !0 },
                label: l(s)("datahub-plugin.simplePricing.price"),
                "error-messages": p(i.value.bulkPrices[a].price),
                type: "number"
              }, null, 8, ["modelValue", "onUpdate:modelValue", "label", "error-messages"])
            ]),
            "cell-fromQuantity": n(({ rowIndex: a }) => [
              g(l(y), {
                modelValue: i.value.bulkPrices[a].fromQuantity,
                "onUpdate:modelValue": (f) => i.value.bulkPrices[a].fromQuantity = f,
                modelModifiers: { number: !0 },
                label: l(s)("datahub-plugin.simplePricing.fromQuantity"),
                "error-messages": p(i.value.bulkPrices[a].fromQuantity),
                type: "number"
              }, null, 8, ["modelValue", "onUpdate:modelValue", "label", "error-messages"])
            ]),
            "cell-fromDate": n(({ rowIndex: a }) => [
              g(l(y), {
                modelValue: i.value.bulkPrices[a].fromDate,
                "onUpdate:modelValue": (f) => i.value.bulkPrices[a].fromDate = f,
                modelModifiers: { number: !0 },
                label: l(s)("datahub-plugin.simplePricing.fromDate"),
                type: "datetime-local"
              }, null, 8, ["modelValue", "onUpdate:modelValue", "label"])
            ]),
            "cell-toDate": n(({ rowIndex: a }) => [
              g(l(y), {
                modelValue: i.value.bulkPrices[a].toDate,
                "onUpdate:modelValue": (f) => i.value.bulkPrices[a].toDate = f,
                modelModifiers: { number: !0 },
                label: l(s)("datahub-plugin.simplePricing.toDate"),
                type: "datetime-local"
              }, null, 8, ["modelValue", "onUpdate:modelValue", "label"])
            ]),
            _: 1
          }, 16)
        ];
      }),
      _: 1
    }, 8, ["modelValue", "title"]));
  }
});
function N(m) {
  const o = m;
  o.__i18n = o.__i18n || [], o.__i18n.push({
    locale: "",
    resource: {
      "en-US": {
        "datahub-plugin": {
          simplePricing: {
            confirmDeleteBulkPrice: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Do you really want to delete the scale price?" } },
            invalidNumber: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Enter a valid number" } },
            msrp: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "MSRP" } },
            basePriceCalculation: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Base price calculation" } },
            edit: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Edit price" } },
            headerText: { t: 0, b: { t: 2, i: [{ t: 3, v: "Currency " }, { t: 4, k: "currency" }, { t: 3, v: ", all prices are " }, { t: 4, k: "type" }] } },
            currentPricingRange: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Current price range" } },
            numberScalePrices: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "# Scale prices" } },
            basePrice: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Base price" } },
            price: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Price" } },
            fromQuantity: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "From" } },
            fromDate: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "from Date" } },
            toDate: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "to Date" } },
            deleteConfig: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Do you really want to remove the price?" } }
          }
        }
      },
      "de-DE": {
        "datahub-plugin": {
          simplePricing: {
            confirmDeleteBulkPrice: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Möchten Sie den Staffelpreis wirklich löschen?" } },
            invalidNumber: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Ungültige Zahl" } },
            msrp: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "UVP" } },
            basePriceCalculation: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Basispreis bearbeiten" } },
            edit: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Preis bearbeiten" } },
            headerText: { t: 0, b: { t: 2, i: [{ t: 3, v: "Währung " }, { t: 4, k: "currency" }, { t: 3, v: ", alle Preise sind " }, { t: 4, k: "type" }] } },
            currentPricingRange: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Derzeitige Preisspanne" } },
            numberScalePrices: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "# Staffelpreise" } },
            basePrice: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Basispreis" } },
            price: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Preis" } },
            fromQuantity: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Ab Menge" } },
            fromDate: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "von" } },
            toDate: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "bis" } },
            deleteConfig: { t: 0, b: { t: 2, i: [{ t: 3 }], s: "Möchten Sie den Preis wirklich entfernen?" } }
          }
        }
      }
    }
  });
}
typeof N == "function" && N(W);
const X = /* @__PURE__ */ O({
  __name: "DatahubDataTypeSimplePricingValueEditor",
  props: /* @__PURE__ */ B({
    modelValue: {},
    readOnly: { type: Boolean },
    attribute: {},
    massEditing: { type: Boolean },
    label: {},
    fieldName: {},
    dataTypeInstance: {},
    loading: { type: Boolean },
    displayOnlyPrice: { type: Boolean }
  }, {
    modelValue: { default: D },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ B(["validated", "update:model-value"], ["update:modelValue"]),
  setup(m) {
    const o = m, u = L(m, "modelValue"), { t: v } = G(), P = w(!1), s = k(() => {
      var t, r;
      const c = (r = (t = o.attribute) == null ? void 0 : t.configuration) != null && r.isGross ? "gross" : "net", e = o.displayOnlyPrice ? void 0 : v("datahub-plugin.simplePricing.headerText", {
        currency: h.value,
        type: c
      });
      return `${o.massEditing ? "" : `${o.label}. `} ${e}`;
    }), i = k(() => {
      var c, e;
      return {
        ...C,
        data: u.value ? [u.value] : [],
        tableName: s.value,
        isLoading: o.loading,
        readonly: o.readOnly || ((e = (c = o.attribute) == null ? void 0 : c.configuration) == null ? void 0 : e.readOnly),
        createActions: !u.value,
        columns: o.displayOnlyPrice ? C.columns.slice(0, 1) : C.columns
      };
    }), h = k(
      () => {
        var c, e;
        return ((e = (c = o.attribute) == null ? void 0 : c.configuration) == null ? void 0 : e.currencyCode) ?? "EUR";
      }
    );
    function M(c) {
      let e = Number(c.basePrice), t = e;
      return Array.isArray(c.bulkPrices) && c.bulkPrices.forEach(({ price: r }) => {
        r > t && (t = Number(r)), r < e && (e = Number(r));
      }), `${e} - ${t} ${h.value}`;
    }
    function p() {
      P.value = !0, u.value = D();
    }
    function S() {
      P.value = !0;
    }
    function E() {
      confirm(v("datahub-plugin.simplePricing.deleteConfig")) && (u.value = D());
    }
    return (c, e) => (T(), U(l(I), x({ class: "simple-pricing" }, i.value, {
      onCreate: p,
      onRemove: E
    }), {
      "header-basePrice": n(({ column: t }) => [
        b(d(l(v)(t.header)), 1)
      ]),
      "header-price": n(({ column: t }) => [
        b(d(l(v)(t.header)), 1)
      ]),
      "header-bulkPrices": n(({ column: t }) => [
        b(d(l(v)(t.header)), 1)
      ]),
      "cell-basePrice": n(({ cellValue: t }) => [
        b(d(t ? `${t} ${h.value}` : ""), 1)
      ]),
      "cell-price": n(({ row: t }) => [
        b(d(M(t)), 1)
      ]),
      "cell-bulkPrices": n(({ cellValue: t }) => [
        b(d((t == null ? void 0 : t.length) ?? 0), 1)
      ]),
      "cell-actions": n(() => [
        g(l(F), {
          icon: "edit",
          variant: "text",
          onClick: S
        })
      ]),
      modals: n(() => {
        var t;
        return [
          g(W, {
            modelValue: P.value,
            "onUpdate:modelValue": e[0] || (e[0] = (r) => P.value = r),
            config: (t = c.attribute) == null ? void 0 : t.configuration,
            value: u.value,
            currency: h.value,
            readonly: c.readOnly,
            onSave: e[1] || (e[1] = (r) => u.value = r)
          }, null, 8, ["modelValue", "config", "value", "currency", "readonly"])
        ];
      }),
      _: 1
    }, 16));
  }
});
typeof N == "function" && N(X);
export {
  X as default
};
