import { defineComponent as ie, mergeModels as W, useModel as le, computed as v, ref as B, watch as E, onBeforeUnmount as ae, inject as ue, resolveComponent as b, openBlock as d, createElementBlock as y, createElementVNode as u, createVNode as h, withCtx as F, renderSlot as G, normalizeClass as j, toDisplayString as _, withDirectives as re, vModelText as me, createCommentVNode as I, createTextVNode as ce, createBlock as U, Fragment as de, renderList as pe } from "vue";
import { l as fe } from "./lodash-TzWV2CbR.js";
import { w as D } from "./app-Bpb4394e.js";
import { g as O, c as ve, a as M, t as be, e as ye, m as he, b as Me, u as ge } from "./number-BVg7EKPV.js";
import { I as R, a as xe } from "./rules-CoimOM7X.js";
import "@feedmepos/ui-library";
function H() {
  return {
    amount: D.reset()
  };
}
function J() {
  return function(f) {
    return R()(f.amountAsString);
  };
}
function We(a) {
  return function(t) {
    return R()(t.amountAsString) !== !0 ? !0 : +t.amountAsString < a ? `Must be at least ${a}` : !0;
  };
}
function Ee(a) {
  return function(t) {
    return R()(t.amountAsString) !== !0 ? !0 : +t.amountAsString > a ? `Must be at most ${a}` : !0;
  };
}
function Z(a, f = !1) {
  return function(c) {
    const s = J()(c);
    if (s != !0)
      return f ? s : !0;
    const A = typeof a == "number" ? a : O(a, c.modelValue.measurement);
    return ve(+c.amountAsString).precision > A ? `Cannot exceed precision more than ${M({
      amount: 1,
      precision: A
    })}` : !0;
  };
}
function Ge(a) {
  const f = xe(a);
  return function(c) {
    return f(c.modelValue.amount);
  };
}
const Fe = { class: "flex flex-col" }, _e = { class: "flex flex-col" }, Ae = { class: "flex-1" }, Pe = { class: "flex items-center gap-4" }, ke = { class: "flex-1 flex gap-4" }, Be = { class: "shrink-0 grow basis-2/5" }, Ie = {
  key: 0,
  class: "text-fm-color-typo-secondary"
}, Ue = {
  key: 0,
  class: "flex"
}, De = { class: "line-clamp-1 text-ellipsis break-all" }, Re = { class: "flex items-center gap-4" }, we = { class: "flex items-center" }, Ce = { class: "line-clamp-1 text-ellipsis break-all" }, Se = { class: "max-w-[200px] flex flex-col gap-[1px]" }, Ne = {
  key: 1,
  class: "flex flex-col gap-4 pl-8 shrink-0"
}, Ve = {
  class: "flex items-center",
  style: { "pointer-events": "none" }
}, Te = {
  class: "flex items-center",
  style: { "pointer-events": "none" }
}, He = /* @__PURE__ */ ie({
  __name: "FmUnitInput",
  props: /* @__PURE__ */ W({
    modelValue: {},
    unit: {},
    rules: {},
    zIndex: {},
    applyDefaultRules: { type: Boolean },
    placement: {},
    separator: {},
    precision: {},
    convertAmountOnChangeUnit: { type: Boolean },
    formatMinimumPrecision: {},
    disableUnit: { type: Boolean },
    stepperOnly: { type: Boolean },
    stepper: {},
    placeholder: {},
    minLength: {},
    maxLength: {},
    inputmode: {},
    type: {},
    readonly: { type: Boolean },
    labelMark: {},
    prependIcon: {},
    appendIcon: {},
    showValidBorder: { type: Boolean },
    labelInfo: {},
    showWordCount: { type: Boolean },
    invalid: { type: Boolean },
    autofocus: { type: Boolean },
    formatter: { type: Function },
    decimal: {},
    datalist: {},
    shiftDatalist: { type: Boolean },
    datalistMaxHeight: {},
    datalistMinHeight: {},
    offsetDatalist: {},
    datalistPlacement: {},
    datalistZIndex: {},
    datalistWidth: {},
    label: {},
    disabled: { type: Boolean },
    focused: { type: Boolean },
    helperText: {},
    helperState: {}
  }, {
    modelValue: {},
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ W(["update:amount", "update:measurement"], ["update:modelValue"]),
  setup(a, { emit: f }) {
    const t = a, c = f, s = le(a, "modelValue"), A = R(), w = v(() => {
      var n;
      return O(t.unit, (n = s.value) == null ? void 0 : n.measurement);
    }), p = B((() => {
      var o;
      const e = ((o = s.value) == null ? void 0 : o.amount) ?? D.reset(), n = Me(e), i = t.formatMinimumPrecision ?? n.precision, l = ge(n, i);
      return M(l);
    })());
    let x = null;
    E(p, (e) => {
      x && clearTimeout(x), x = setTimeout(() => {
        var r;
        const n = A(e), i = t.precision ?? w.value, o = typeof t.formatMinimumPrecision == "number" && t.formatMinimumPrecision > i ? t.formatMinimumPrecision : i;
        if (n == !0) {
          const g = be(+e, o);
          ((r = s.value) == null ? void 0 : r.amount) !== g && (s.value = {
            ...s.value,
            amount: g
          });
        }
        c("update:amount", e);
      }, 500);
    }), ae(() => {
      x && clearTimeout(x);
    });
    const Q = Symbol.for("fm:form-state"), C = ue(Q, null), P = v(() => t.disabled ?? (C == null ? void 0 : C.getDisabled()) ?? !1), X = B(), S = B(!1), $ = B(!1), Y = v(() => S.value || $.value), ee = v(() => {
      var l;
      const e = t.unit.measurements.filter((o) => !o.name.toLocaleLowerCase().includes("inactive")).filter((o) => o.abbrev !== t.unit.abbrev || +M(o.conversion) != 1), n = fe.groupBy(
        e,
        (o) => `${o.abbrev}${M(o.conversion)}`
      ), i = Object.values(n).map((o) => o[0]);
      return [
        {
          label: (l = t.unit) == null ? void 0 : l.abbrev,
          value: null,
          isBaseUnit: !0
        },
        ...i.map((o) => ({
          label: o.abbrev,
          value: o.id,
          isBaseUnit: !1
        }))
      ];
    }), N = v({
      get() {
        var e, n;
        return ((n = (e = s.value) == null ? void 0 : e.measurement) == null ? void 0 : n.id) ?? null;
      },
      set(e) {
        if (e == null)
          s.value = {
            ...s.value ?? H(),
            measurement: null
          }, c("update:measurement", null);
        else {
          const n = t.unit.measurements.find((i) => i.id === e) ?? null;
          s.value = {
            ...s.value ?? H(),
            measurement: n
          }, c("update:measurement", n);
        }
      }
    });
    E(N, (e, n) => {
      var m;
      if (!t.convertAmountOnChangeUnit)
        return;
      const i = ((m = s.value) == null ? void 0 : m.amount) ?? D.reset(), l = t.unit.measurements.find((k) => k.id === n) ?? null, o = t.unit.measurements.find((k) => k.id === e) ?? null, r = l ? ye(i, l.conversion) : i, g = o ? he(r, o.conversion) : r, T = O(t.unit, o), L = D.convertPrecision({
        value: g,
        precision: T
      });
      p.value = M(L);
    });
    const q = v(() => {
      var e;
      return !((e = s.value) != null && e.measurement);
    }), V = v(() => {
      var e, n;
      return ((n = (e = s.value) == null ? void 0 : e.measurement) == null ? void 0 : n.abbrev) ?? t.unit.abbrev;
    }), te = v(
      () => {
        var e;
        return ((e = t.rules) == null ? void 0 : e.map(
          (n) => (i) => typeof n == "function" ? n(i) : n
        )) ?? [];
      }
    ), ne = v(() => [...t.applyDefaultRules ? [
      J(),
      typeof t.precision == "number" ? Z(t.precision) : Z(t.unit)
    ] : [], ...te.value]);
    function z(e) {
      const n = t.precision ?? w.value, l = typeof t.formatMinimumPrecision == "number" && t.formatMinimumPrecision > n ? t.formatMinimumPrecision : n, o = e.toFixed(l), r = o.includes(".") ? o.replace(/0+$/, "") : o;
      return r.endsWith(".") ? r.slice(0, r.length - 1) : r;
    }
    function oe() {
      if (!t.stepper)
        return;
      const e = +p.value + +M(t.stepper);
      p.value = z(e);
    }
    function se() {
      if (!t.stepper)
        return;
      const e = +p.value - +M(t.stepper);
      p.value = z(e);
    }
    return (e, n) => {
      const i = b("FmLabel"), l = b("FmIcon"), o = b("FmMenuItem"), r = b("FmMenu"), g = b("FmTooltip"), T = b("FmField"), L = b("FmFormGroup");
      return d(), y("div", Fe, [
        u("div", _e, [
          u("div", Ae, [
            h(i, {
              label: e.label,
              disabled: e.disabled,
              required: e.labelMark === "required",
              optional: e.labelMark === "optional"
            }, null, 8, ["label", "disabled", "required", "optional"])
          ]),
          h(T, { focused: Y.value }, {
            default: F(() => [
              u("div", Pe, [
                G(e.$slots, "prepend"),
                u("div", ke, [
                  u("div", Be, [
                    e.stepperOnly ? (d(), y("span", {
                      key: 0,
                      class: j({
                        "fm-text-field--input fm-typo-en-body-lg-400": !0,
                        "text-fm-color-typo-primary": !P.value,
                        "text-fm-color-typo-disabled": P.value
                      })
                    }, _(p.value), 3)) : re((d(), y("input", {
                      key: 1,
                      type: "text",
                      ref_key: "inputRef",
                      ref: X,
                      onFocus: n[0] || (n[0] = () => S.value = !0),
                      onBlur: n[1] || (n[1] = () => S.value = !1),
                      "onUpdate:modelValue": n[2] || (n[2] = (m) => p.value = m),
                      class: j({
                        "fm-text-field--input fm-typo-en-body-lg-400": !0,
                        "text-fm-color-typo-primary": !P.value,
                        "text-fm-color-typo-disabled": P.value
                      })
                    }, null, 34)), [
                      [me, p.value]
                    ])
                  ]),
                  e.separator ? (d(), y("div", Ie, _(e.separator), 1)) : I("", !0),
                  u("div", {
                    class: j([e.disabled ? "text-fm-color-typo-disabled" : "text-fm-color-typo-secondary"])
                  }, [
                    h(g, { "z-index": 51 }, {
                      content: F(() => [
                        ce(_(V.value), 1)
                      ]),
                      default: F(() => [
                        e.disableUnit ? (d(), y("div", Ue, [
                          q.value ? I("", !0) : (d(), U(l, {
                            key: 0,
                            name: "square_foot"
                          })),
                          u("div", De, _(V.value), 1)
                        ])) : (d(), U(r, {
                          key: 1,
                          onMenuChanged: n[3] || (n[3] = (m) => $.value = m),
                          "z-index": 500,
                          placement: e.placement,
                          disabled: e.disabled
                        }, {
                          "menu-button": F(() => [
                            u("div", Re, [
                              u("div", we, [
                                q.value ? I("", !0) : (d(), U(l, {
                                  key: 0,
                                  name: "square_foot"
                                })),
                                u("div", Ce, _(V.value), 1)
                              ]),
                              h(l, { name: "keyboard_arrow_down" })
                            ])
                          ]),
                          default: F(() => [
                            u("div", Se, [
                              (d(!0), y(de, null, pe(ee.value, (m) => (d(), U(o, {
                                key: m.value ?? void 0,
                                label: m.label,
                                icon: m.isBaseUnit ? void 0 : "square_foot",
                                "model-value": m.value == N.value,
                                onClick: (k) => N.value = m.value
                              }, null, 8, ["label", "icon", "model-value", "onClick"]))), 128))
                            ])
                          ]),
                          _: 1
                        }, 8, ["placement", "disabled"]))
                      ]),
                      _: 1
                    })
                  ], 2),
                  e.stepper ? (d(), y("div", Ne, [
                    u("div", {
                      class: "w-24 h-12 flex items-center justify-center fm-corner-radius-sm bg-fm-color-neutral-gray-100 hover:bg-fm-color-neutral-gray-200",
                      onClick: oe
                    }, [
                      u("div", Ve, [
                        h(l, {
                          name: "expand_less",
                          size: "md"
                        })
                      ])
                    ]),
                    u("div", {
                      class: "w-24 h-12 flex items-center justify-center fm-corner-radius-sm bg-fm-color-neutral-gray-100 hover:bg-fm-color-neutral-gray-200",
                      onClick: se
                    }, [
                      u("div", Te, [
                        h(l, {
                          name: "expand_more",
                          size: "md"
                        })
                      ])
                    ])
                  ])) : I("", !0)
                ]),
                G(e.$slots, "append")
              ])
            ]),
            _: 3
          }, 8, ["focused"])
        ]),
        h(L, {
          class: "flex-1",
          "model-value": { modelValue: s.value, amountAsString: String(p.value) },
          info: e.labelInfo,
          rules: ne.value,
          "helper-text": e.helperText,
          "helper-state": e.helperState
        }, null, 8, ["model-value", "info", "rules", "helper-text", "helper-state"])
      ]);
    };
  }
});
export {
  Ge as F,
  He as _,
  We as a,
  Ee as b,
  J as c,
  Z as d,
  H as e
};
