import { defineStore as Q } from "pinia";
import { defineComponent as T, computed as U, ref as h, watch as L, resolveComponent as V, createBlock as W, openBlock as F, withCtx as w, createVNode as m, createElementVNode as f, normalizeClass as H, createElementBlock as A, toDisplayString as M, unref as l, Fragment as q, renderSlot as X, renderList as Z, getCurrentInstance as ee } from "vue";
import { a8 as P, a9 as te, a6 as B, I as k, Q as $, G as I, D as E, a7 as ne, M as C, A as D, J as oe, u as ae } from "./index.vue_vue_type_script_setup_true_lang-BMVa5UfU.js";
import { useI18n as N } from "@feedmepos/mf-common";
import { useSnackbar as j, components as le, useDialog as ue, FmButtonVariant as ie } from "@feedmepos/ui-library";
import { u as se } from "./discardConfirm-BHni0LNu.js";
import { c as re } from "./check-C9mY6LiX.js";
import "./vuedraggable-CK8z7qA8.js";
import "./menuV2-Da02vc32.js";
import "./app-5fxKLAzS.js";
/* empty css                                                                              */
import { c as me } from "./createComponentProgrammatically-Bt2rn1IQ.js";
const de = ["value", "disabled"], ce = /* @__PURE__ */ T({
  __name: "NumberPrecisionInput",
  props: {
    modelValue: {},
    label: {},
    helperText: {},
    step: {},
    rules: {},
    disabled: { type: Boolean }
  },
  emits: ["update:modelValue"],
  setup(y, { emit: g }) {
    const t = y, n = g, c = U(() => P(t.step ?? 1).precision), d = h(t.modelValue.toString());
    L([() => d.value], ([e]) => {
      e === "-" || e === "." ? n("update:modelValue", 0) : n("update:modelValue", +e);
    });
    const b = (e) => {
      if (e.ctrlKey || e.altKey || e.metaKey || ["Backspace", "Tab", "Enter", "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(
        e.key
      ))
        return;
      const i = e.currentTarget, a = +`${i.value}${e.key}`;
      if (isFinite(a)) {
        P(a).precision > c.value && e.preventDefault();
        return;
      }
      i.value.length === 0 && (e.key === "-" || e.key === ".") || e.preventDefault();
    };
    return (e, i) => {
      const a = V("FmField"), o = V("FmFormGroup");
      return F(), W(o, {
        disabled: e.disabled,
        modelValue: e.modelValue,
        rules: e.rules,
        "helper-text": e.helperText,
        label: e.label
      }, {
        default: w(({ invalid: p }) => [
          m(a, { invalid: p }, {
            default: w(() => [
              f("input", {
                value: d.value,
                onInput: i[0] || (i[0] = (_) => d.value = _.target.value),
                onKeydown: b,
                inputmode: "numeric",
                class: H({
                  "fm-text-field--input fm-typo-en-body-lg-400": !0,
                  "text-fm-color-typo-primary": !e.disabled,
                  "text-fm-color-typo-disabled": e.disabled
                }),
                disabled: e.disabled
              }, null, 42, de)
            ]),
            _: 2
          }, 1032, ["invalid"])
        ]),
        _: 1
      }, 8, ["disabled", "modelValue", "rules", "helper-text", "label"]);
    };
  }
}), pe = { class: "flex flex-col gap-8" }, ve = { class: "flex justify-between" }, fe = { class: "col q-mb-sm t-body-bold" }, be = { class: "flex flex-row gap-8" }, _e = { class: "q-mt-xs q-pl-sm t-system text-fsecondary" }, Ve = /* @__PURE__ */ T({
  __name: "UnitMeasurement",
  props: {
    title: {
      type: String,
      required: !0
    },
    unitName: {
      type: String,
      required: !0
    },
    precision: {
      type: Number,
      required: !0
    },
    modelValue: {
      type: Object,
      required: !0
    }
  },
  emits: ["update:model-value", "delete"],
  setup(y, { emit: g }) {
    const { t } = N(), n = y, c = U(() => te(n.modelValue.conversion)), d = U(() => t("menu.unit.measurement.preview", {
      name: n.modelValue.name,
      value: c.value,
      unit: n.unitName
    }));
    function b(a) {
      e("conversion", B(a, n.precision));
    }
    function e(a, o) {
      const p = k(n.modelValue);
      p[a] = o, i("update:model-value", p);
    }
    const i = g;
    return L(
      () => n.precision,
      () => b(c.value)
    ), (a, o) => {
      const p = V("FmButton"), _ = V("FmTextField");
      return F(), A("div", pe, [
        f("div", ve, [
          f("div", fe, M(y.title), 1),
          m(p, {
            icon: "delete",
            variant: "secondary",
            onClick: o[0] || (o[0] = (s) => i("delete"))
          })
        ]),
        f("div", be, [
          m(_, {
            "model-value": n.modelValue.name,
            rules: [
              (s) => s ? !0 : l(t)("menu.common.validation.required", {
                field: l(t)("menu.unit.measurement.name").toLowerCase()
              })
            ],
            label: l(t)("menu.unit.measurement.name"),
            "onUpdate:modelValue": o[1] || (o[1] = (s) => e("name", s.trim()))
          }, null, 8, ["model-value", "rules", "label"]),
          m(_, {
            "model-value": n.modelValue.abbrev,
            rules: [
              (s) => s ? !0 : l(t)("menu.common.validation.required", {
                field: l(t)("menu.unit.measurement.symbol").toLowerCase()
              })
            ],
            label: l(t)("menu.unit.measurement.symbol"),
            "onUpdate:modelValue": o[2] || (o[2] = (s) => e("abbrev", s.trim()))
          }, null, 8, ["model-value", "rules", "label"])
        ]),
        m(ce, {
          label: l(t)("menu.unit.measurement.conversion"),
          "model-value": c.value,
          "onUpdate:modelValue": b,
          step: 1 / Math.pow(10, n.precision)
        }, null, 8, ["label", "model-value", "step"]),
        f("div", _e, M(l(t)("menu.unit.measurement.preview_label")) + ": " + M(d.value), 1)
      ]);
    };
  }
}), ye = { class: "fm-typo-en-title-md-600" }, ge = { class: "pt-8 space-y-40" }, Fe = { class: "flex flex-row gap-16" }, we = { class: "flex gap-8 items-center justify-start" }, Ue = /* @__PURE__ */ T({
  __name: "Form",
  props: {
    initialValue: {}
  },
  setup(y, { expose: g }) {
    const { t } = N(), n = y, c = $(I(j())), d = U(() => n.initialValue ? t("menu.unit.update") : t("menu.unit.add")), b = U(
      () => n.initialValue ? t("menu.common.actions.update") : t("menu.common.actions.add")
    ), e = h(n.initialValue ? k(n.initialValue) : E.generator.initUnit()), i = h(!1), a = h(!1), o = (u) => {
      re.isBoolean(u) ? a.value = u : a.value = !a.value;
    };
    function p() {
      e.value.measurements = e.value.measurements.map((u) => ({
        ...u,
        conversion: ne(u.conversion, e.value.precision)
      }));
    }
    function _() {
      e.value.measurements.push({
        id: (/* @__PURE__ */ new Date()).toISOString(),
        name: "",
        abbrev: "",
        conversion: B(0, e.value.precision)
      });
    }
    function s(u) {
      e.value.measurements.splice(u, 1);
    }
    const K = se(
      () => !oe.isEqual(e.value, n.initialValue || E.generator.initUnit()),
      () => {
        a.value = !1;
      }
    ), Y = async () => {
      try {
        i.value = !0;
        const u = k(e.value);
        n.initialValue ? await c.updateMenu(
          {
            module: D.F_MENU_MODULE_V4_KEY.enum.unit,
            operation: C.F_OPERATION_TYPE.update,
            data: u
          },
          u.name
        ) : await c.updateMenu(
          {
            module: D.F_MENU_MODULE_V4_KEY.enum.unit,
            operation: C.F_OPERATION_TYPE.create,
            data: u
          },
          u.name
        ), o(!1), e.value = E.generator.initUnit();
      } finally {
        i.value = !1;
      }
    };
    return L(
      () => a.value,
      (u) => {
        u && (e.value = n.initialValue ? k(n.initialValue) : E.generator.initUnit());
      }
    ), g({
      show: a,
      trigger: o
    }), (u, v) => {
      const O = V("FmTextField"), R = V("FmStepperField"), z = V("FmButton"), S = V("fm-button"), G = V("fm-side-sheet");
      return F(), A(q, null, [
        X(u.$slots, "default", { trigger: o }),
        m(G, {
          id: "unit-form",
          modelValue: a.value,
          "onUpdate:modelValue": v[4] || (v[4] = (r) => a.value = r),
          "is-container": l(le).FmForm,
          "is-container-props": {
            onValidationSuccess: Y,
            blameFormChildOnValidationFailed: !0
          },
          "max-width": 560,
          "onOn:clickedAway": l(K)
        }, {
          "side-sheet-header": w(() => [
            f("p", ye, M(d.value), 1)
          ]),
          "side-sheet-footer": w(() => [
            f("div", we, [
              m(S, {
                label: b.value,
                loading: i.value,
                size: "lg",
                type: "submit",
                variant: "primary"
              }, null, 8, ["label", "loading"]),
              m(S, {
                label: l(t)("menu.common.actions.cancel"),
                size: "lg",
                variant: "tertiary",
                onClick: v[3] || (v[3] = () => a.value = !1)
              }, null, 8, ["label"])
            ])
          ]),
          default: w(() => [
            f("div", ge, [
              f("div", Fe, [
                m(O, {
                  class: "flex-2",
                  label: l(t)("menu.unit.columns.name"),
                  modelValue: e.value.name,
                  "onUpdate:modelValue": v[0] || (v[0] = (r) => e.value.name = r),
                  rules: [
                    (r) => r ? !0 : l(t)("menu.common.validation.required", {
                      field: l(t)("menu.unit.columns.name").toLowerCase()
                    })
                  ]
                }, null, 8, ["label", "modelValue", "rules"]),
                m(O, {
                  class: "flex-1",
                  label: l(t)("menu.unit.columns.symbol"),
                  modelValue: e.value.abbrev,
                  "onUpdate:modelValue": v[1] || (v[1] = (r) => e.value.abbrev = r),
                  rules: [
                    (r) => r ? !0 : l(t)("menu.common.validation.required", {
                      field: l(t)("menu.unit.columns.symbol").toLowerCase()
                    })
                  ]
                }, null, 8, ["label", "modelValue", "rules"])
              ]),
              f("div", null, [
                m(R, {
                  modelValue: e.value.precision,
                  "onUpdate:modelValue": [
                    v[2] || (v[2] = (r) => e.value.precision = r),
                    p
                  ],
                  label: l(t)("menu.unit.precision"),
                  placeholder: l(t)("menu.unit.precision_placeholder"),
                  step: 1,
                  min: 0,
                  max: 5
                }, null, 8, ["modelValue", "label", "placeholder"])
              ]),
              m(z, {
                label: l(t)("menu.unit.measurement.add"),
                variant: "secondary",
                onClick: _
              }, null, 8, ["label"]),
              (F(!0), A(q, null, Z(e.value.measurements, (r, x) => (F(), A("div", {
                key: r.id
              }, [
                m(Ve, {
                  modelValue: e.value.measurements[x],
                  "onUpdate:modelValue": (J) => e.value.measurements[x] = J,
                  "unit-name": e.value.name,
                  title: l(t)("menu.unit.measurement.title", { number: x + 1 }),
                  precision: e.value.precision,
                  onDelete: () => s(x)
                }, null, 8, ["modelValue", "onUpdate:modelValue", "unit-name", "title", "precision", "onDelete"])
              ]))), 128))
            ])
          ]),
          _: 1
        }, 8, ["modelValue", "is-container", "is-container-props", "onOn:clickedAway"])
      ], 64);
    };
  }
}), xe = Ue, qe = Q("unit-manager", () => {
  const y = ue(), g = j(), t = I(g), n = $(t);
  ae();
  const c = ee(), { t: d } = N(), b = (o) => {
    var _, s;
    const { vNode: p } = me(
      xe,
      c,
      o
    );
    (s = (_ = p.component) == null ? void 0 : _.exposed) == null || s.trigger();
  };
  async function e() {
    b({});
  }
  async function i(o) {
    b({
      initialValue: o
    });
  }
  async function a(o) {
    y.open({
      title: d("menu.common.dialog.remove.unit.title"),
      message: d("menu.common.dialog.remove.unit.message", { name: o.name }),
      primaryActions: {
        text: d("menu.common.actions.remove"),
        close: !0,
        variant: ie.Destructive
      },
      secondaryActions: {
        text: d("menu.common.actions.cancel"),
        close: !0
      }
    }).onPrimary(async () => {
      await n.updateMenu(
        {
          module: D.F_MENU_MODULE_V4_KEY.enum.unit,
          operation: C.F_OPERATION_TYPE.delete,
          data: o._id
        },
        o.name,
        !0
      );
    });
  }
  return {
    createUnit: e,
    deleteUnit: a,
    updateUnit: i
  };
});
export {
  xe as U,
  qe as u
};
