import { defineComponent as le, mergeModels as W, useModel as ae, computed as v, ref as k, watch as E, onBeforeUnmount as ue, inject as re, resolveComponent as b, openBlock as d, createElementBlock as y, createElementVNode as r, createVNode as h, withCtx as F, renderSlot as H, normalizeClass as O, toDisplayString as _, withDirectives as me, vModelText as ce, createCommentVNode as I, createTextVNode as de, createBlock as U, Fragment as pe, renderList as fe } from "vue";
import { l as ve } from "./netsuite-DmXOUJo-.js";
import { w as D } from "./app-D3gm3Lsw.js";
import { g as $, c as be, a as x, t as ye, e as he, m as xe, b as Me, u as ge } from "./number-BfNWSAkN.js";
import { I as R, a as Fe } from "./rules-BK8yTx5p.js";
import "@feedmepos/ui-library";
import { u as _e, a as Ae } from "./uom-rules-BxHGqiP7.js";
function G() {
  return {
    amount: D.reset()
  };
}
function J() {
  return function(f) {
    return R()(f.amountAsString);
  };
}
function Ze(a) {
  return function(t) {
    return R()(t.amountAsString) !== !0 ? !0 : +t.amountAsString < a ? `Must be at least ${a}` : !0;
  };
}
function Je(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 : $(a, c.modelValue.measurement);
    return be(+c.amountAsString).precision > A ? `Cannot exceed precision more than ${x({
      amount: 1,
      precision: A
    })}` : !0;
  };
}
function Ke(a) {
  const f = Fe(a);
  return function(c) {
    return f(c.modelValue.amount);
  };
}
const Be = { class: "flex flex-col" }, Pe = { class: "flex flex-col" }, ke = { class: "flex-1" }, Ie = { class: "flex items-center gap-4" }, Ue = { class: "flex-1 flex gap-4" }, De = { class: "shrink-0 grow basis-2/5" }, Re = {
  key: 0,
  class: "text-fm-color-typo-secondary"
}, Ce = {
  key: 0,
  class: "flex"
}, we = { class: "line-clamp-1 text-ellipsis break-all" }, Se = { class: "flex items-center gap-4" }, Ne = { class: "flex items-center" }, Ve = { class: "line-clamp-1 text-ellipsis break-all" }, Te = { class: "max-w-[200px] flex flex-col gap-[1px]" }, je = {
  key: 1,
  class: "flex flex-col gap-4 pl-8 shrink-0"
}, Oe = {
  class: "flex items-center",
  style: { "pointer-events": "none" }
}, $e = {
  class: "flex items-center",
  style: { "pointer-events": "none" }
}, Qe = /* @__PURE__ */ le({
  __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 = ae(a, "modelValue"), A = R(), C = v(() => {
      var n;
      return $(t.unit, (n = s.value) == null ? void 0 : n.measurement);
    }), p = k((() => {
      var i;
      const e = ((i = s.value) == null ? void 0 : i.amount) ?? D.reset(), n = Me(e), l = t.formatMinimumPrecision ?? n.precision, u = ge(n, l);
      return x(u);
    })());
    let g = null;
    E(p, (e) => {
      g && clearTimeout(g), g = setTimeout(() => {
        var o;
        const n = A(e), l = t.precision ?? C.value, i = typeof t.formatMinimumPrecision == "number" && t.formatMinimumPrecision > l ? t.formatMinimumPrecision : l;
        if (n == !0) {
          const M = ye(+e, i);
          ((o = s.value) == null ? void 0 : o.amount) !== M && (s.value = {
            ...s.value,
            amount: M
          });
        }
        c("update:amount", e);
      }, 500);
    }), ue(() => {
      g && clearTimeout(g);
    });
    const Q = Symbol.for("fm:form-state"), w = re(Q, null), B = v(() => t.disabled ?? (w == null ? void 0 : w.getDisabled()) ?? !1), X = k(), S = k(!1), q = k(!1), Y = v(() => S.value || q.value), ee = _e(), te = v(() => {
      var i;
      const e = ee.value, n = t.unit.measurements.filter((o) => !Ae(o, e)).filter((o) => o.abbrev !== t.unit.abbrev || +x(o.conversion) != 1), l = ve.groupBy(
        n,
        (o) => `${o.abbrev}${x(o.conversion)}`
      ), u = Object.values(l).map((o) => o[0]);
      return [
        {
          label: (i = t.unit) == null ? void 0 : i.abbrev,
          value: null,
          isBaseUnit: !0
        },
        ...u.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 ?? G(),
            measurement: null
          }, c("update:measurement", null);
        else {
          const n = t.unit.measurements.find((l) => l.id === e) ?? null;
          s.value = {
            ...s.value ?? G(),
            measurement: n
          }, c("update:measurement", n);
        }
      }
    });
    E(N, (e, n) => {
      var m;
      if (!t.convertAmountOnChangeUnit)
        return;
      const l = ((m = s.value) == null ? void 0 : m.amount) ?? D.reset(), u = t.unit.measurements.find((P) => P.id === n) ?? null, i = t.unit.measurements.find((P) => P.id === e) ?? null, o = u ? he(l, u.conversion) : l, M = i ? xe(o, i.conversion) : o, T = $(t.unit, i), j = D.convertPrecision({
        value: M,
        precision: T
      });
      p.value = x(j);
    });
    const L = 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;
    }), ne = v(
      () => {
        var e;
        return ((e = t.rules) == null ? void 0 : e.map(
          (n) => (l) => typeof n == "function" ? n(l) : n
        )) ?? [];
      }
    ), oe = v(() => [...t.applyDefaultRules ? [
      J(),
      typeof t.precision == "number" ? Z(t.precision) : Z(t.unit)
    ] : [], ...ne.value]);
    function z(e) {
      const n = t.precision ?? C.value, u = typeof t.formatMinimumPrecision == "number" && t.formatMinimumPrecision > n ? t.formatMinimumPrecision : n, i = e.toFixed(u), o = i.includes(".") ? i.replace(/0+$/, "") : i;
      return o.endsWith(".") ? o.slice(0, o.length - 1) : o;
    }
    function se() {
      if (!t.stepper)
        return;
      const e = +p.value + +x(t.stepper);
      p.value = z(e);
    }
    function ie() {
      if (!t.stepper)
        return;
      const e = +p.value - +x(t.stepper);
      p.value = z(e);
    }
    return (e, n) => {
      const l = b("FmLabel"), u = b("FmIcon"), i = b("FmMenuItem"), o = b("FmMenu"), M = b("FmTooltip"), T = b("FmField"), j = b("FmFormGroup");
      return d(), y("div", Be, [
        r("div", Pe, [
          r("div", ke, [
            h(l, {
              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(() => [
              r("div", Ie, [
                H(e.$slots, "prepend"),
                r("div", Ue, [
                  r("div", De, [
                    e.stepperOnly ? (d(), y("span", {
                      key: 0,
                      class: O({
                        "fm-text-field--input fm-typo-en-body-lg-400": !0,
                        "text-fm-color-typo-primary": !B.value,
                        "text-fm-color-typo-disabled": B.value
                      })
                    }, _(p.value), 3)) : me((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: O({
                        "fm-text-field--input fm-typo-en-body-lg-400": !0,
                        "text-fm-color-typo-primary": !B.value,
                        "text-fm-color-typo-disabled": B.value
                      })
                    }, null, 34)), [
                      [ce, p.value]
                    ])
                  ]),
                  e.separator ? (d(), y("div", Re, _(e.separator), 1)) : I("", !0),
                  r("div", {
                    class: O([e.disabled ? "text-fm-color-typo-disabled" : "text-fm-color-typo-secondary"])
                  }, [
                    h(M, { "z-index": 51 }, {
                      content: F(() => [
                        de(_(V.value), 1)
                      ]),
                      default: F(() => [
                        e.disableUnit ? (d(), y("div", Ce, [
                          L.value ? I("", !0) : (d(), U(u, {
                            key: 0,
                            name: "square_foot"
                          })),
                          r("div", we, _(V.value), 1)
                        ])) : (d(), U(o, {
                          key: 1,
                          onMenuChanged: n[3] || (n[3] = (m) => q.value = m),
                          "z-index": 500,
                          placement: e.placement,
                          disabled: e.disabled
                        }, {
                          "menu-button": F(() => [
                            r("div", Se, [
                              r("div", Ne, [
                                L.value ? I("", !0) : (d(), U(u, {
                                  key: 0,
                                  name: "square_foot"
                                })),
                                r("div", Ve, _(V.value), 1)
                              ]),
                              h(u, { name: "keyboard_arrow_down" })
                            ])
                          ]),
                          default: F(() => [
                            r("div", Te, [
                              (d(!0), y(pe, null, fe(te.value, (m) => (d(), U(i, {
                                key: m.value ?? void 0,
                                label: m.label,
                                icon: m.isBaseUnit ? void 0 : "square_foot",
                                "model-value": m.value == N.value,
                                onClick: (P) => N.value = m.value
                              }, null, 8, ["label", "icon", "model-value", "onClick"]))), 128))
                            ])
                          ]),
                          _: 1
                        }, 8, ["placement", "disabled"]))
                      ]),
                      _: 1
                    })
                  ], 2),
                  e.stepper ? (d(), y("div", je, [
                    r("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
                    }, [
                      r("div", Oe, [
                        h(u, {
                          name: "expand_less",
                          size: "md"
                        })
                      ])
                    ]),
                    r("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: ie
                    }, [
                      r("div", $e, [
                        h(u, {
                          name: "expand_more",
                          size: "md"
                        })
                      ])
                    ])
                  ])) : I("", !0)
                ]),
                H(e.$slots, "append")
              ])
            ]),
            _: 3
          }, 8, ["focused"])
        ]),
        h(j, {
          class: "flex-1",
          "model-value": { modelValue: s.value, amountAsString: String(p.value) },
          info: e.labelInfo,
          rules: oe.value,
          "helper-text": e.helperText,
          "helper-state": e.helperState
        }, null, 8, ["model-value", "info", "rules", "helper-text", "helper-state"])
      ]);
    };
  }
});
export {
  Ke as F,
  Qe as _,
  Ze as a,
  Je as b,
  J as c,
  Z as d,
  G as e
};
