import { defineComponent as $, mergeModels as C, useModel as A, ref as O, computed as h, watch as F, createBlock as U, openBlock as E, unref as l, withCtx as r, createElementVNode as W, createVNode as f, createCommentVNode as j, toDisplayString as c, mergeProps as L, createTextVNode as p } from "vue";
import { useI18n as w } from "vue-i18n";
import { X as H, d as y, j as x, I as G, a0 as T } from "./start-CE9SawgU.js";
import "pinia";
import "./header-Bn1EKnBE.js";
import { c as K } from "./index-C0QCq-hK.js";
const V = () => ({
  basePrice: null,
  msrp: null,
  bulkPrices: []
}), Q = () => ({ fromQuantity: null, price: null }), k = (s) => typeof s == "string" ? Number(s.replace(",", ".")) : s, D = {
  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 } }
    }
  ]
}, _ = {
  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"
    }
  ]
}, I = /* @__PURE__ */ $({
  __name: "EditPricingModal",
  props: /* @__PURE__ */ C({
    config: {},
    currency: {},
    value: {},
    readonly: { type: Boolean }
  }, {
    modelValue: { type: Boolean },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ C(["save"], ["update:modelValue"]),
  setup(s, { emit: o }) {
    const u = s, P = o, g = A(s, "modelValue"), { t: n } = w(), t = O(V()), v = h(() => !!(d(t.value.basePrice) || u.config?.msrp && d(t.value.msrp) || t.value.bulkPrices.some(
      ({ price: a, fromQuantity: i }) => d(a) || d(i)
    ))), N = h(() => ({
      ...D,
      data: t.value.bulkPrices,
      createActions: !u.readonly,
      columns: u.config?.promotions ? _.columns : _.columns.slice(0, 2)
    })), d = (a) => !a || isNaN(Number(a)) ? n("datahub-plugin.simplePricing.invalidNumber") : void 0;
    function S() {
      t.value.bulkPrices ? t.value.bulkPrices.push(Q()) : t.value.bulkPrices = [Q()];
    }
    function M({ rowIndex: a }) {
      confirm(n("datahub-plugin.simplePricing.confirmDeleteBulkPrice")) && t.value.bulkPrices.splice(a, 1);
    }
    function m() {
      const a = {
        basePrice: k(t.value.basePrice),
        bulkPrices: t.value.bulkPrices.map(
          ({ fromQuantity: i, price: e, fromDate: b, toDate: z }) => {
            const R = {
              fromQuantity: k(i),
              price: k(e)
            };
            return u.config?.promotions && (R.fromDate = T(b, !1), R.toDate = T(z, !0)), R;
          }
        )
      };
      u.config?.msrp && (a.msrp = k(t.value.msrp)), g.value = !1, P("save", a);
    }
    return F(
      () => u.value,
      (a) => {
        t.value = K(a);
      },
      { immediate: !0 }
    ), (a, i) => (E(), U(l(H), {
      modelValue: g.value,
      "onUpdate:modelValue": i[2] || (i[2] = (e) => g.value = e),
      title: l(n)("datahub-plugin.simplePricing.edit"),
      "max-width": "600px"
    }, {
      actions: r(() => [
        f(l(G), {
          text: a.$t("common.saveChanges"),
          disabled: v.value,
          color: "primary",
          onClick: m
        }, null, 8, ["text", "disabled"])
      ]),
      default: r(() => [
        W("div", null, c(l(n)("datahub-plugin.simplePricing.basePriceCalculation")), 1),
        f(l(y), {
          modelValue: t.value.basePrice,
          "onUpdate:modelValue": i[0] || (i[0] = (e) => t.value.basePrice = e),
          modelModifiers: { number: !0 },
          label: l(n)("datahub-plugin.simplePricing.basePrice"),
          "error-messages": d(t.value.basePrice),
          type: "number"
        }, null, 8, ["modelValue", "label", "error-messages"]),
        a.config?.msrp ? (E(), U(l(y), {
          key: 0,
          modelValue: t.value.msrp,
          "onUpdate:modelValue": i[1] || (i[1] = (e) => t.value.msrp = e),
          modelModifiers: { number: !0 },
          label: l(n)("datahub-plugin.simplePricing.msrp"),
          "error-messages": d(t.value.msrp),
          type: "number"
        }, null, 8, ["modelValue", "label", "error-messages"])) : j("", !0),
        f(l(x), L(N.value, {
          onCreate: S,
          onRemove: M
        }), {
          "header-price": r(({ column: e }) => [
            p(c(l(n)(e.header)), 1)
          ]),
          "header-fromQuantity": r(({ column: e }) => [
            p(c(l(n)(e.header)), 1)
          ]),
          "header-fromDate": r(({ column: e }) => [
            p(c(l(n)(e.header)), 1)
          ]),
          "header-toDate": r(({ column: e }) => [
            p(c(l(n)(e.header)), 1)
          ]),
          "cell-price": r(({ rowIndex: e }) => [
            f(l(y), {
              modelValue: t.value.bulkPrices[e].price,
              "onUpdate:modelValue": (b) => t.value.bulkPrices[e].price = b,
              modelModifiers: { number: !0 },
              label: l(n)("datahub-plugin.simplePricing.price"),
              "error-messages": d(t.value.bulkPrices[e].price),
              type: "number"
            }, null, 8, ["modelValue", "onUpdate:modelValue", "label", "error-messages"])
          ]),
          "cell-fromQuantity": r(({ rowIndex: e }) => [
            f(l(y), {
              modelValue: t.value.bulkPrices[e].fromQuantity,
              "onUpdate:modelValue": (b) => t.value.bulkPrices[e].fromQuantity = b,
              modelModifiers: { number: !0 },
              label: l(n)("datahub-plugin.simplePricing.fromQuantity"),
              "error-messages": d(t.value.bulkPrices[e].fromQuantity),
              type: "number"
            }, null, 8, ["modelValue", "onUpdate:modelValue", "label", "error-messages"])
          ]),
          "cell-fromDate": r(({ rowIndex: e }) => [
            f(l(y), {
              modelValue: t.value.bulkPrices[e].fromDate,
              "onUpdate:modelValue": (b) => t.value.bulkPrices[e].fromDate = b,
              modelModifiers: { number: !0 },
              label: l(n)("datahub-plugin.simplePricing.fromDate"),
              type: "datetime-local"
            }, null, 8, ["modelValue", "onUpdate:modelValue", "label"])
          ]),
          "cell-toDate": r(({ rowIndex: e }) => [
            f(l(y), {
              modelValue: t.value.bulkPrices[e].toDate,
              "onUpdate:modelValue": (b) => t.value.bulkPrices[e].toDate = b,
              modelModifiers: { number: !0 },
              label: l(n)("datahub-plugin.simplePricing.toDate"),
              type: "datetime-local"
            }, null, 8, ["modelValue", "onUpdate:modelValue", "label"])
          ]),
          _: 1
        }, 16)
      ]),
      _: 1
    }, 8, ["modelValue", "title"]));
  }
});
function B(s) {
  const o = s;
  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 B == "function" && B(I);
const X = /* @__PURE__ */ $({
  __name: "DatahubDataTypeSimplePricingValueEditor",
  props: /* @__PURE__ */ C({
    modelValue: {},
    readOnly: { type: Boolean },
    attribute: {},
    massEditing: { type: Boolean },
    label: {},
    fieldName: {},
    dataTypeInstance: {},
    loading: { type: Boolean },
    succeeded: { type: Boolean },
    displayOnlyPrice: { type: Boolean }
  }, {
    modelValue: { default: V },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ C(["validated", "update:model-value", "initialized"], ["update:modelValue"]),
  setup(s) {
    const o = s, u = A(s, "modelValue"), { t: P } = w(), g = O(!1), n = h(() => {
      const m = o.attribute?.configuration?.isGross ? "gross" : "net", a = o.displayOnlyPrice ? void 0 : P("datahub-plugin.simplePricing.headerText", {
        currency: v.value,
        type: m
      });
      return `${o.massEditing ? "" : `${o.label}. `} ${a}`;
    }), t = h(() => ({
      ...D,
      data: u.value ? [u.value] : [],
      tableName: n.value,
      isLoading: o.loading,
      readonly: o.readOnly || o.attribute?.configuration?.readOnly,
      createActions: !u.value,
      columns: o.displayOnlyPrice ? D.columns.slice(0, 1) : D.columns
    })), v = h(
      () => o.attribute?.configuration?.currencyCode ?? "EUR"
    );
    function N(m) {
      let a = Number(m.basePrice), i = a;
      return Array.isArray(m.bulkPrices) && m.bulkPrices.forEach(({ price: e }) => {
        e > i && (i = Number(e)), e < a && (a = Number(e));
      }), `${a} - ${i} ${v.value}`;
    }
    function d() {
      g.value = !0, u.value = V();
    }
    function S() {
      g.value = !0;
    }
    function M() {
      confirm(P("datahub-plugin.simplePricing.deleteConfig")) && (u.value = V());
    }
    return (m, a) => (E(), U(l(x), L({ class: "simple-pricing" }, t.value, {
      onCreate: d,
      onRemove: M
    }), {
      "header-basePrice": r(({ column: i }) => [
        p(c(l(P)(i.header)), 1)
      ]),
      "header-price": r(({ column: i }) => [
        p(c(l(P)(i.header)), 1)
      ]),
      "header-bulkPrices": r(({ column: i }) => [
        p(c(l(P)(i.header)), 1)
      ]),
      "cell-basePrice": r(({ cellValue: i }) => [
        p(c(i ? `${i} ${v.value}` : ""), 1)
      ]),
      "cell-price": r(({ row: i }) => [
        p(c(N(i)), 1)
      ]),
      "cell-bulkPrices": r(({ cellValue: i }) => [
        p(c(i?.length ?? 0), 1)
      ]),
      "cell-actions": r(() => [
        f(l(G), {
          icon: "lucide:pencil",
          variant: "text",
          onClick: S
        })
      ]),
      modals: r(() => [
        f(I, {
          modelValue: g.value,
          "onUpdate:modelValue": a[0] || (a[0] = (i) => g.value = i),
          config: m.attribute?.configuration,
          value: u.value,
          currency: v.value,
          readonly: m.readOnly,
          onSave: a[1] || (a[1] = (i) => u.value = i)
        }, null, 8, ["modelValue", "config", "value", "currency", "readonly"])
      ]),
      _: 1
    }, 16));
  }
});
typeof B == "function" && B(X);
export {
  X as default
};
