import { ref as D, getCurrentInstance as P, version as R, computed as O, watch as S, defineComponent as C, useAttrs as M, openBlock as $, createBlock as A, mergeProps as L, createSlots as T, withCtx as G, createVNode as w } from "vue";
import { isEmpty as k } from "es-toolkit/compat";
import B from "./Icon.js";
import W from "./Input.js";
/**
 * Vue Currency Input 3.1.0
 * (c) 2018-2024 Matthias Stiller
 * @license MIT
 */
var y;
(function(r) {
  r.symbol = "symbol", r.narrowSymbol = "narrowSymbol", r.code = "code", r.name = "name", r.hidden = "hidden";
})(y || (y = {}));
var v;
(function(r) {
  r.precision = "precision", r.thousands = "thousands", r.millions = "millions", r.billions = "billions";
})(v || (v = {}));
const x = (r) => r.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), I = (r) => r.replace(/^0+(0$|[^0])/, "$1"), V = (r, i) => (r.match(new RegExp(x(i), "g")) || []).length, z = (r, i) => r.substring(0, r.indexOf(i)), _ = [
  ",",
  ".",
  "٫",
  "。"
], E = "(0|[1-9]\\d*)";
class j {
  constructor(i) {
    var t, e, n, d, a, l;
    const { currency: m, currencyDisplay: g, locale: o, precision: u, accountingSign: h, useGrouping: c } = i;
    this.locale = o, this.options = {
      currency: m,
      useGrouping: c,
      style: "currency",
      currencySign: h ? "accounting" : void 0,
      currencyDisplay: g !== y.hidden ? g : void 0
    };
    const s = new Intl.NumberFormat(o, this.options), f = s.formatToParts(123456);
    this.currency = (t = f.find(({ type: p }) => p === "currency")) === null || t === void 0 ? void 0 : t.value, this.digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((p) => p.toLocaleString(o)), this.decimalSymbol = (e = f.find(({ type: p }) => p === "decimal")) === null || e === void 0 ? void 0 : e.value, this.groupingSymbol = (n = f.find(({ type: p }) => p === "group")) === null || n === void 0 ? void 0 : n.value, this.minusSign = (d = s.formatToParts(-1).find(({ type: p }) => p === "minusSign")) === null || d === void 0 ? void 0 : d.value, this.decimalSymbol === void 0 ? this.minimumFractionDigits = this.maximumFractionDigits = 0 : typeof u == "number" ? this.minimumFractionDigits = this.maximumFractionDigits = u : (this.minimumFractionDigits = (a = u == null ? void 0 : u.min) !== null && a !== void 0 ? a : s.resolvedOptions().minimumFractionDigits, this.maximumFractionDigits = (l = u == null ? void 0 : u.max) !== null && l !== void 0 ? l : s.resolvedOptions().maximumFractionDigits);
    const b = (p) => z(p, this.digits[1]), F = (p) => p.substring(p.lastIndexOf(this.decimalSymbol ? this.digits[0] : this.digits[1]) + 1);
    this.prefix = b(s.format(1)), this.suffix = F(s.format(1)), this.negativePrefix = b(s.format(-1)), this.negativeSuffix = F(s.format(-1));
  }
  parse(i) {
    if (i) {
      const t = this.isNegative(i);
      i = this.normalizeDigits(i), i = this.stripCurrency(i, t), i = this.stripSignLiterals(i);
      const e = this.decimalSymbol ? `(?:${x(this.decimalSymbol)}(\\d*))?` : "", n = this.stripGroupingSeparator(i).match(new RegExp(`^${E}${e}$`));
      if (n && this.isValidIntegerFormat(this.decimalSymbol ? i.split(this.decimalSymbol)[0] : i, Number(n[1])))
        return +`${t ? "-" : ""}${this.onlyDigits(n[1])}.${this.onlyDigits(n[2] || "")}`;
    }
    return null;
  }
  isValidIntegerFormat(i, t) {
    const e = { ...this.options, minimumFractionDigits: 0 };
    return [
      this.stripCurrency(this.normalizeDigits(t.toLocaleString(this.locale, { ...e, useGrouping: !0 })), !1),
      this.stripCurrency(this.normalizeDigits(t.toLocaleString(this.locale, { ...e, useGrouping: !1 })), !1)
    ].includes(i);
  }
  format(i, t = {
    minimumFractionDigits: this.minimumFractionDigits,
    maximumFractionDigits: this.maximumFractionDigits
  }) {
    return i != null ? i.toLocaleString(this.locale, { ...this.options, ...t }) : "";
  }
  toFraction(i) {
    return `${this.digits[0]}${this.decimalSymbol}${this.onlyLocaleDigits(i.substring(1)).substring(0, this.maximumFractionDigits)}`;
  }
  isFractionIncomplete(i) {
    return !!this.normalizeDigits(this.stripGroupingSeparator(i)).match(new RegExp(`^${E}${x(this.decimalSymbol)}$`));
  }
  isNegative(i) {
    return i.startsWith(this.negativePrefix) || this.minusSign === void 0 && (i.startsWith("(") || i.startsWith("-")) || this.minusSign !== void 0 && i.replace("-", this.minusSign).startsWith(this.minusSign);
  }
  insertCurrency(i, t) {
    return `${t ? this.negativePrefix : this.prefix}${i}${t ? this.negativeSuffix : this.suffix}`;
  }
  stripGroupingSeparator(i) {
    return this.groupingSymbol !== void 0 ? i.replace(new RegExp(x(this.groupingSymbol), "g"), "") : i;
  }
  stripSignLiterals(i) {
    return this.minusSign !== void 0 ? i.replace("-", this.minusSign).replace(this.minusSign, "") : i.replace(/[-()]/g, "");
  }
  stripCurrency(i, t) {
    return i.replace(t ? this.negativePrefix : this.prefix, "").replace(t ? this.negativeSuffix : this.suffix, "");
  }
  normalizeDecimalSeparator(i, t) {
    return _.forEach((e) => {
      i = i.substring(0, t) + i.substring(t).replace(e, this.decimalSymbol);
    }), i;
  }
  normalizeDigits(i) {
    return this.digits[0] !== "0" && this.digits.forEach((t, e) => {
      i = i.replace(new RegExp(t, "g"), String(e));
    }), i;
  }
  onlyDigits(i) {
    return this.normalizeDigits(i).replace(/\D+/g, "");
  }
  onlyLocaleDigits(i) {
    return i.replace(new RegExp(`[^${this.digits.join("")}]*`, "g"), "");
  }
}
class N {
  constructor(i) {
    this.currencyFormat = i;
  }
}
class X extends N {
  conformToMask(i, t = "") {
    const e = this.currencyFormat.isNegative(i), n = (s) => s === "" && e && !(this.currencyFormat.minusSign === void 0 ? t === this.currencyFormat.negativePrefix + this.currencyFormat.negativeSuffix : t === this.currencyFormat.negativePrefix), d = (s) => {
      if (n(s))
        return "";
      if (this.currencyFormat.maximumFractionDigits > 0) {
        if (this.currencyFormat.isFractionIncomplete(s))
          return s;
        if (s.startsWith(this.currencyFormat.decimalSymbol))
          return this.currencyFormat.toFraction(s);
      }
      return null;
    };
    let a = i;
    a = this.currencyFormat.stripCurrency(a, e), a = this.currencyFormat.stripSignLiterals(a);
    const l = d(a);
    if (l != null)
      return this.currencyFormat.insertCurrency(l, e);
    const [m, ...g] = a.split(this.currencyFormat.decimalSymbol), o = I(this.currencyFormat.onlyDigits(m)), u = this.currencyFormat.onlyDigits(g.join("")).substring(0, this.currencyFormat.maximumFractionDigits), h = g.length > 0 && u.length === 0, c = o === "" && e && (this.currencyFormat.minusSign === void 0 ? t === i.slice(0, -2) + this.currencyFormat.negativeSuffix : t === i.slice(0, -1));
    return h || c || n(o) ? t : o.match(/\d+/) ? {
      numberValue: +`${e ? "-" : ""}${o}.${u}`,
      fractionDigits: u
    } : "";
  }
}
class U extends N {
  conformToMask(i, t = "") {
    if (i === "" || this.currencyFormat.parse(t) === 0 && this.currencyFormat.stripCurrency(t, !0).slice(0, -1) === this.currencyFormat.stripCurrency(i, !0))
      return "";
    const e = this.currencyFormat.isNegative(i), n = this.currencyFormat.stripSignLiterals(i) === "" ? -0 : +`${e ? "-" : ""}${I(this.currencyFormat.onlyDigits(i))}` / Math.pow(10, this.currencyFormat.maximumFractionDigits);
    return {
      numberValue: n,
      fractionDigits: n.toFixed(this.currencyFormat.maximumFractionDigits).slice(-this.currencyFormat.maximumFractionDigits)
    };
  }
}
const q = {
  locale: void 0,
  currency: void 0,
  currencyDisplay: void 0,
  hideGroupingSeparatorOnFocus: !0,
  hideCurrencySymbolOnFocus: !0,
  hideNegligibleDecimalDigitsOnFocus: !0,
  precision: void 0,
  autoDecimalDigits: !1,
  valueRange: void 0,
  useGrouping: void 0,
  valueScaling: void 0
};
class Y {
  constructor(i) {
    this.el = i.el, this.onInput = i.onInput, this.onChange = i.onChange, this.addEventListener(), this.init(i.options);
  }
  setOptions(i) {
    this.init(i), this.format(this.currencyFormat.format(this.validateValueRange(this.numberValue))), this.onChange(this.getValue());
  }
  getValue() {
    return { number: this.valueScaling && this.numberValue != null ? this.toInteger(this.numberValue, this.valueScaling) : this.numberValue, formatted: this.formattedValue };
  }
  setValue(i) {
    const t = this.valueScaling !== void 0 && i != null ? this.toFloat(i, this.valueScaling) : i;
    t !== this.numberValue && (this.format(this.currencyFormat.format(this.validateValueRange(t))), this.onChange(this.getValue()));
  }
  init(i) {
    this.options = {
      ...q,
      ...i
    }, this.options.autoDecimalDigits && (this.options.hideNegligibleDecimalDigitsOnFocus = !1), this.el.getAttribute("inputmode") || this.el.setAttribute("inputmode", this.options.autoDecimalDigits ? "numeric" : "decimal"), this.currencyFormat = new j(this.options), this.numberMask = this.options.autoDecimalDigits ? new U(this.currencyFormat) : new X(this.currencyFormat);
    const t = {
      [v.precision]: this.currencyFormat.maximumFractionDigits,
      [v.thousands]: 3,
      [v.millions]: 6,
      [v.billions]: 9
    };
    this.valueScaling = this.options.valueScaling ? t[this.options.valueScaling] : void 0, this.valueScalingFractionDigits = this.valueScaling !== void 0 && this.options.valueScaling !== v.precision ? this.valueScaling + this.currencyFormat.maximumFractionDigits : this.currencyFormat.maximumFractionDigits, this.minValue = this.getMinValue(), this.maxValue = this.getMaxValue();
  }
  getMinValue() {
    var i, t;
    let e = this.toFloat(-Number.MAX_SAFE_INTEGER);
    return ((i = this.options.valueRange) === null || i === void 0 ? void 0 : i.min) !== void 0 && (e = Math.max((t = this.options.valueRange) === null || t === void 0 ? void 0 : t.min, this.toFloat(-Number.MAX_SAFE_INTEGER))), e;
  }
  getMaxValue() {
    var i, t;
    let e = this.toFloat(Number.MAX_SAFE_INTEGER);
    return ((i = this.options.valueRange) === null || i === void 0 ? void 0 : i.max) !== void 0 && (e = Math.min((t = this.options.valueRange) === null || t === void 0 ? void 0 : t.max, this.toFloat(Number.MAX_SAFE_INTEGER))), e;
  }
  toFloat(i, t) {
    return i / Math.pow(10, t ?? this.valueScalingFractionDigits);
  }
  toInteger(i, t) {
    return Number(i.toFixed(t ?? this.valueScalingFractionDigits).split(".").join(""));
  }
  validateValueRange(i) {
    return i != null ? Math.min(Math.max(i, this.minValue), this.maxValue) : i;
  }
  format(i, t = !1) {
    if (i != null) {
      this.decimalSymbolInsertedAt !== void 0 && (i = this.currencyFormat.normalizeDecimalSeparator(i, this.decimalSymbolInsertedAt), this.decimalSymbolInsertedAt = void 0);
      const e = this.numberMask.conformToMask(i, this.formattedValue);
      let n;
      if (typeof e == "object") {
        const { numberValue: d, fractionDigits: a } = e;
        let { maximumFractionDigits: l, minimumFractionDigits: m } = this.currencyFormat;
        this.focus ? m = t ? a.replace(/0+$/, "").length : Math.min(l, a.length) : Number.isInteger(d) && !this.options.autoDecimalDigits && (this.options.precision === void 0 || m === 0) && (m = l = 0), n = this.toInteger(Math.abs(d)) > Number.MAX_SAFE_INTEGER ? this.formattedValue : this.currencyFormat.format(d, {
          useGrouping: this.options.useGrouping !== !1 && !(this.focus && this.options.hideGroupingSeparatorOnFocus),
          minimumFractionDigits: m,
          maximumFractionDigits: l
        });
      } else
        n = e;
      this.maxValue <= 0 && !this.currencyFormat.isNegative(n) && this.currencyFormat.parse(n) !== 0 && (n = n.replace(this.currencyFormat.prefix, this.currencyFormat.negativePrefix)), this.minValue >= 0 && (n = n.replace(this.currencyFormat.negativePrefix, this.currencyFormat.prefix)), (this.options.currencyDisplay === y.hidden || this.focus && this.options.hideCurrencySymbolOnFocus) && (n = n.replace(this.currencyFormat.negativePrefix, this.currencyFormat.minusSign !== void 0 ? this.currencyFormat.minusSign : "(").replace(this.currencyFormat.negativeSuffix, this.currencyFormat.minusSign !== void 0 ? "" : ")").replace(this.currencyFormat.prefix, "").replace(this.currencyFormat.suffix, "")), this.el.value = n, this.numberValue = this.currencyFormat.parse(n);
    } else
      this.el.value = "", this.numberValue = null;
    this.formattedValue = this.el.value, this.onInput(this.getValue());
  }
  addEventListener() {
    this.el.addEventListener("input", (i) => {
      const { value: t, selectionStart: e } = this.el, n = i;
      if (e && n.data && _.includes(n.data) && (this.decimalSymbolInsertedAt = e - 1), this.format(t), this.focus && e != null) {
        const d = () => {
          const { prefix: a, suffix: l, decimalSymbol: m, maximumFractionDigits: g, groupingSymbol: o } = this.currencyFormat;
          let u = t.length - e;
          const h = this.formattedValue.length;
          if (this.currencyFormat.minusSign === void 0 && (t.startsWith("(") || t.startsWith("-")) && !t.endsWith(")"))
            return h - this.currencyFormat.negativeSuffix.length > 1 ? this.formattedValue.substring(e).length : 1;
          if (this.formattedValue.substring(e, 1) === o && V(this.formattedValue, o) === V(t, o) + 1)
            return h - u - 1;
          if (h < u)
            return e;
          if (m !== void 0 && t.indexOf(m) !== -1) {
            const c = t.indexOf(m) + 1;
            if (Math.abs(h - t.length) > 1 && e <= c)
              return this.formattedValue.indexOf(m) + 1;
            !this.options.autoDecimalDigits && e > c && this.currencyFormat.onlyDigits(t.substring(c)).length - 1 === g && (u -= 1);
          }
          return this.options.hideCurrencySymbolOnFocus || this.options.currencyDisplay === y.hidden ? h - u : Math.max(h - Math.max(u, l.length), a.length);
        };
        this.setCaretPosition(d());
      }
    }), this.el.addEventListener("focus", () => {
      this.focus = !0, this.numberValueOnFocus = this.numberValue, setTimeout(() => {
        const { value: i, selectionStart: t, selectionEnd: e } = this.el;
        if (this.format(i, this.options.hideNegligibleDecimalDigitsOnFocus), t != null && e != null && Math.abs(t - e) > 0)
          this.setCaretPosition(0, this.el.value.length);
        else if (t != null) {
          const n = this.getCaretPositionOnFocus(i, t);
          this.setCaretPosition(n);
        }
      });
    }), this.el.addEventListener("blur", () => {
      this.focus = !1, this.format(this.currencyFormat.format(this.validateValueRange(this.numberValue))), this.numberValueOnFocus !== this.numberValue && this.onChange(this.getValue());
    });
  }
  getCaretPositionOnFocus(i, t) {
    if (this.numberValue == null)
      return t;
    const { prefix: e, negativePrefix: n, suffix: d, negativeSuffix: a, groupingSymbol: l, currency: m } = this.currencyFormat, g = this.numberValue < 0, o = g ? n : e, u = o.length;
    if (this.options.hideCurrencySymbolOnFocus || this.options.currencyDisplay === y.hidden) {
      if (g) {
        if (t <= 1)
          return 1;
        if (i.endsWith(")") && t > i.indexOf(")"))
          return this.formattedValue.length - 1;
      }
    } else {
      const c = g ? a.length : d.length;
      if (t >= i.length - c)
        return this.formattedValue.length - c;
      if (t < u)
        return u;
    }
    let h = t;
    return this.options.hideCurrencySymbolOnFocus && this.options.currencyDisplay !== y.hidden && t >= u && m !== void 0 && o.includes(m) && (h -= u, g && (h += 1)), this.options.hideGroupingSeparatorOnFocus && l !== void 0 && (h -= V(i.substring(0, t), l)), h;
  }
  setCaretPosition(i, t = i) {
    this.el.setSelectionRange(i, t);
  }
}
const Z = (r) => r != null && r.matches("input") ? r : r == null ? void 0 : r.querySelector("input");
function H(r, i) {
  var t, e, n, d;
  let a;
  const l = D(null), m = D(null), g = D(null), o = P();
  o != null && o.emit || ((e = (t = o == null ? void 0 : o.proxy) === null || t === void 0 ? void 0 : t.$emit) === null || e === void 0 || e.bind(o == null ? void 0 : o.proxy));
  const u = (o == null ? void 0 : o.props) || ((n = o == null ? void 0 : o.proxy) === null || n === void 0 ? void 0 : n.$props), h = R.startsWith("3");
  h && ((d = o == null ? void 0 : o.attrs.modelModifiers) === null || d === void 0 || d.lazy);
  const c = O(() => u == null ? void 0 : u[h ? "modelValue" : "value"]);
  return S(l, (s) => {
    var f;
    if (s) {
      const b = Z((f = s == null ? void 0 : s.$el) !== null && f !== void 0 ? f : s);
      b ? (a = new Y({
        el: b,
        options: r,
        onInput: (F) => {
          g.value = F.number, m.value = F.formatted;
        },
        onChange: (F) => {
        }
      }), a.setValue(c.value)) : console.error('No input element found. Please make sure that the "inputRef" template ref is properly assigned.');
    } else
      a = null;
  }), {
    inputRef: l,
    numberValue: g,
    formattedValue: m,
    setValue: (s) => a == null ? void 0 : a.setValue(s),
    setOptions: (s) => a == null ? void 0 : a.setOptions(s)
  };
}
const ti = /* @__PURE__ */ C({
  __name: "CurrencyInput",
  props: {
    inputOptions: { default: void 0 },
    modelValue: { default: null },
    hidePrepend: { type: Boolean, default: !1 },
    autocomplete: {},
    value: {},
    placeholder: {},
    addBottomSpace: { type: Boolean },
    errorText: { default: void 0 },
    hintText: { default: void 0 },
    id: { default: void 0 },
    errorId: {},
    isReadOnly: { type: Boolean },
    isRequired: { type: Boolean },
    label: { default: void 0 },
    showOptionalInLabel: { type: Boolean },
    isLabelSrOnly: { type: Boolean },
    fieldset: { type: Boolean },
    isDisabled: { type: Boolean },
    disabled: { type: Boolean }
  },
  emits: ["update:model-value"],
  setup(r, { expose: i, emit: t }) {
    const e = {
      currency: "USD",
      currencyDisplay: y.hidden,
      precision: 2,
      hideCurrencySymbolOnFocus: !0,
      hideGroupingSeparatorOnFocus: !0,
      hideNegligibleDecimalDigitsOnFocus: !1,
      autoDecimalDigits: !0,
      useGrouping: !0,
      accountingSign: !1
    }, n = r, d = t, a = M(), l = O(() => ({
      ...e,
      ...k(n.inputOptions) ? {} : n.inputOptions
    })), { inputRef: m, setOptions: g, setValue: o, numberValue: u } = H(l.value);
    i({ inputEl: m }), S(
      () => u.value,
      (c) => {
        var f;
        let s;
        Object.is(c, -0) ? typeof l.value.precision == "number" ? s = "-0." + "0".repeat(l.value.precision) : s = "-0." + "0".repeat(((f = l.value.precision) == null ? void 0 : f.min) || 0) : s = typeof c == "number" ? c.toFixed(2) : c, d("update:model-value", s);
      }
    ), S(
      () => n.modelValue,
      (c) => {
        let s;
        typeof c == "string" ? s = parseFloat(c || "0") : s = c, o(s);
      }
    ), S(
      () => n.inputOptions,
      () => {
        g(l.value);
      }
    );
    const h = O(() => {
      const { inputOptions: c, ...s } = n;
      return { ...s, ...a };
    });
    return (c, s) => ($(), A(W, L(h.value, {
      ref_key: "inputEl",
      ref: m,
      class: "stash-currency-input",
      type: "text",
      "data-test": "stash-currency-input",
      inputmode: "decimal"
    }), T({ _: 2 }, [
      r.hidePrepend ? void 0 : {
        name: "prepend",
        fn: G(() => [
          w(B, { name: "sign-dollar" })
        ]),
        key: "0"
      }
    ]), 1040));
  }
});
export {
  ti as default
};
//# sourceMappingURL=CurrencyInput.js.map
