var He = Object.defineProperty;
var Pe = (F, i, l) => i in F ? He(F, i, { enumerable: !0, configurable: !0, writable: !0, value: l }) : F[i] = l;
var ve = (F, i, l) => Pe(F, typeof i != "symbol" ? i + "" : i, l);
import { defineComponent as be, computed as x, ref as $, watch as ge, openBlock as d, createElementBlock as f, normalizeStyle as z, Fragment as V, renderList as O, renderSlot as ke, createCommentVNode as D, useSlots as Oe, onMounted as Ue, onUnmounted as Ee, reactive as ee, resolveComponent as T, createElementVNode as B, createVNode as k, createTextVNode as Ke, toDisplayString as P, unref as L, createSlots as je, withCtx as w, createBlock as I, resolveDynamicComponent as pe, normalizeClass as fe, withModifiers as Ne, Transition as ze } from "vue";
import { useDialogChild as Ge } from "@feedmepos/ui-library";
import { z as he } from "./app-Bpb4394e.js";
import { d as Je } from "./index-CH0wxGs4.js";
/* empty css                                                                             */
import { useI18n as Re } from "@feedmepos/mf-common";
import { P as We, J as ye, D as Qe } from "./app-DiX-EfV6.js";
import { _ as Xe } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const Ye = /* @__PURE__ */ be({
  __name: "VirtualScroll",
  props: {
    itemHeight: {},
    length: {},
    scrollTop: {},
    prerender: {}
  },
  emits: ["changed:minHeight"],
  setup(F, { emit: i }) {
    const l = F, u = i, a = x(() => {
      const S = l.itemHeight * l.length;
      return u("changed:minHeight", S), `${S}px`;
    }), v = $(0), m = x(
      () => Array.from({ length: l.prerender }).map((S, p) => p + v.value)
    );
    function c() {
      v.value = Math.floor(l.scrollTop / l.itemHeight) - Math.floor(l.prerender / 2);
    }
    const o = $(Je(c, 10));
    ge([() => l.scrollTop], () => {
      o.value();
    });
    function g(S) {
      const p = S.el;
      p && (p.style.opacity = "0", setTimeout(() => {
        p.style.opacity = "1";
      }, 0));
    }
    return (S, p) => (d(), f("div", {
      class: "relative",
      style: z({ minHeight: a.value })
    }, [
      (d(!0), f(V, null, O(m.value, (C) => (d(), f(V, { key: C }, [
        C >= 0 && C < S.length ? (d(), f("div", {
          key: 0,
          class: "absolute w-full left-0 bg-white",
          style: z({
            top: `${S.itemHeight * C}px`,
            height: `${S.itemHeight}px`,
            transition: "opacity 0.5s ease"
          }),
          onVnodeMounted: g
        }, [
          ke(S.$slots, "default", {
            key: C,
            index: C,
            getItem: (A) => A[C]
          })
        ], 4)) : D("", !0)
      ], 64))), 128))
    ], 4));
  }
});
class W {
  static getDescendantProperty(i, l, u = []) {
    let a, v, m, c, o, g;
    if (l) {
      if (m = l.indexOf("."), m === -1 ? a = l : (a = l.slice(0, m), v = l.slice(m + 1)), c = i[a], c !== null && typeof c < "u")
        if (!v && (typeof c == "string" || typeof c == "number"))
          u.push(c);
        else if (Object.prototype.toString.call(c) === "[object Array]")
          for (o = 0, g = c.length; o < g; o++)
            W.getDescendantProperty(c[o], v, u);
        else v && W.getDescendantProperty(c, v, u);
    } else
      u.push(i);
    return u;
  }
}
class U {
  constructor(i = [], l = [], u = {}) {
    Array.isArray(l) || (u = l, l = []), this.haystack = i, this.keys = l, this.options = Object.assign({
      caseSensitive: !1,
      sort: !1
    }, u);
  }
  search(i = "") {
    if (i === "")
      return this.haystack;
    const l = [];
    for (let u = 0; u < this.haystack.length; u++) {
      const a = this.haystack[u];
      if (this.keys.length === 0) {
        const v = U.isMatch(a, i, this.options.caseSensitive);
        v && l.push({ item: a, score: v });
      } else
        for (let v = 0; v < this.keys.length; v++) {
          const m = W.getDescendantProperty(a, this.keys[v]);
          let c = !1;
          for (let o = 0; o < m.length; o++) {
            const g = U.isMatch(m[o], i, this.options.caseSensitive);
            if (g) {
              c = !0, l.push({ item: a, score: g });
              break;
            }
          }
          if (c)
            break;
        }
    }
    return this.options.sort && l.sort((u, a) => u.score - a.score), l.map((u) => u.item);
  }
  static isMatch(i, l, u) {
    i = String(i), l = String(l), u || (i = i.toLocaleLowerCase(), l = l.toLocaleLowerCase());
    const a = U.nearestIndexesFor(i, l);
    return a ? i === l ? 1 : a.length > 1 ? 2 + (a[a.length - 1] - a[0]) : 2 + a[0] : !1;
  }
  static nearestIndexesFor(i, l) {
    const u = l.split("");
    let a = [];
    return U.indexesOfFirstLetter(i, l).forEach((m, c) => {
      let o = m + 1;
      a[c] = [m];
      for (let g = 1; g < u.length; g++) {
        const S = u[g];
        if (o = i.indexOf(S, o), o === -1) {
          a[c] = !1;
          break;
        }
        a[c].push(o), o++;
      }
    }), a = a.filter((m) => m !== !1), a.length ? a.sort((m, c) => m.length === 1 ? m[0] - c[0] : (m = m[m.length - 1] - m[0], c = c[c.length - 1] - c[0], m - c))[0] : !1;
  }
  static indexesOfFirstLetter(i, l) {
    const u = l[0];
    return i.split("").map((a, v) => a !== u ? !1 : v).filter((a) => a !== !1);
  }
}
class Ze {
  constructor(i, l) {
    ve(this, "searcher");
    this.candidates = i, l ?? (l = (u) => [String(u)]), this.searcher = new U(
      i.flatMap(
        (u, a) => (l == null ? void 0 : l(u).map((v) => ({ key: v, index: a }))) ?? []
      ),
      ["key"],
      { caseSensitive: !1, sort: !0 }
    );
  }
  search(i) {
    return [...new Set(this.searcher.search(i).map((l) => l.index))].map(
      (l) => this.candidates[l]
    );
  }
}
const qe = { class: "sticky top-0 bg-white z-50" }, et = {
  key: 0,
  class: "h-12 bg-white absolute top-[-12px] left-[-24px] w-lvw z-50",
  style: { "max-width": "min(100vw - 48px, 560px)" }
}, tt = { class: "flex justify-between items-center" }, lt = { class: "flex gap-16 basis-1/2" }, at = { class: "fm-typo-en-body-lg-400 text-fm-color-typo-secondary" }, st = {
  key: 0,
  class: "ml-4"
}, nt = {
  key: 1,
  class: "pt-8 pb-8"
}, ot = {
  key: 2,
  class: "mt-8 p-12 bg-fm-color-state-warning-subtle border border-fm-color-state-warning-base rounded-4 flex items-center gap-8"
}, it = { class: "fm-typo-en-body-md-400 text-fm-color-typo-primary" }, rt = {
  key: 0,
  class: "flex items-center gap-8 justify-center py-32"
}, ut = { class: "text-fm-color-typo-secondary fm-typo-en-body-md-400" }, dt = {
  key: 0,
  class: "flex gap-4 items-center"
}, ct = {
  key: 2,
  class: "p-16 text-fm-color-typo-disabled fm-typo-en-body-lg-400 text-center"
}, mt = /* @__PURE__ */ be({
  __name: "FmMultiselectDialog",
  props: {
    modelValue: {},
    items: {},
    groups: {},
    singleSelect: { type: Boolean },
    placeholder: {},
    showValidBorder: { type: Boolean },
    maxHeight: {},
    width: {},
    noShadow: { type: Boolean },
    loading: { type: Boolean },
    virtualScroll: { type: Boolean },
    filterAttributes: {},
    attributeKey: {},
    maxSelections: {}
  },
  emits: ["update:modelValue", "close"],
  setup(F, { emit: i }) {
    var oe, ie, re;
    const { t: l } = Re(), u = Ge(), a = F, v = Oe(), m = x(() => {
      var t;
      const e = (t = v.appendList) == null ? void 0 : t.call(v);
      return Array.isArray(e) ? e : e ? [e] : [];
    }), c = i, o = $(a.modelValue ?? []);
    Ue(() => {
      var t, n, r, h;
      u.emitData(o.value);
      const e = (t = a.modelValue) != null && t.length ? a.items.findIndex((_) => {
        var M;
        return he(_.value, (M = a.modelValue) == null ? void 0 : M[0]);
      }) : 0;
      (n = R.value) == null || n.scrollTo({ top: e * 64 - 64, behavior: "smooth" }), (h = (r = ne.value) == null ? void 0 : r.inputEl) == null || h.focus();
    }), Ee(() => {
      c("close");
    }), ge([() => o.value], ([e]) => {
      u.emitData(e), c("update:modelValue", e);
    });
    function g(e) {
      return A.value.has(p(e));
    }
    const S = /* @__PURE__ */ new WeakMap();
    function p(e) {
      if (e && typeof e == "object") {
        if ("_id" in e && e._id) return String(e._id);
        const t = S.get(e);
        if (t) return t;
        const n = JSON.stringify(e);
        return S.set(e, n), n;
      }
      return String(e);
    }
    const C = x(() => {
      const e = /* @__PURE__ */ new Map();
      for (const t of a.items) e.set(p(t.value), t);
      return e;
    }), A = x(() => new Set(o.value.map((e) => p(e)))), Q = x(
      () => new Set(b.value.map((e) => p(e.value)))
    );
    function H(e, t) {
      if (a.singleSelect) {
        o.value = t ? [e] : [];
        return;
      }
      const n = p(e), r = A.value.has(n);
      if (!(t && r || !t && !r))
        if (t) {
          if (a.maxSelections && o.value.length >= a.maxSelections)
            return;
          o.value = [...o.value, e];
        } else
          o.value = o.value.filter((h) => p(h) !== n);
    }
    const xe = x(
      () => new Ze(
        a.items,
        (e) => [e.label ?? "", e.sublabel ?? "", ...e.searchKeys ?? []].filter((t) => t)
      )
    ), E = $(""), G = $(null), b = x(() => {
      let e = xe.value.search(E.value);
      if (G.value && a.filterAttributes) {
        const t = We.build(G.value);
        e = a.attributeKey ? e.filter((n) => ye(n[a.attributeKey] ?? {}, t)) : e.filter((n) => ye(n, t));
      }
      return e;
    }), te = x(() => {
      const e = [], t = C.value, n = Q.value;
      for (const r of o.value) {
        const h = p(r), _ = t.get(h);
        _ && !_.disabled && n.has(h) && e.push(r);
      }
      return e;
    }), K = x(() => {
      const e = b.value.filter((t) => !t.disabled);
      return te.value.length === e.length;
    }), le = x(() => te.value.length > 0 && !K.value);
    function Se() {
      if (K.value) {
        const e = Q.value;
        o.value = o.value.filter((t) => {
          const n = p(t), r = C.value.get(n);
          return (r == null ? void 0 : r.disabled) && !e.has(n);
        });
      } else {
        const e = A.value, t = Q.value;
        o.value = a.items.filter(
          (n) => !n.disabled && (e.has(p(n.value)) || t.has(p(n.value)))
        ).map((n) => n.value);
      }
    }
    const _e = ee(
      ((oe = a.items) == null ? void 0 : oe.map((e) => ({ value: e.value, disabled: e.disabled ?? !1 }))) ?? []
    );
    function we(e, t) {
      return e.reduce(
        (n, r) => {
          const h = t(r);
          return n[h] || (n[h] = []), n[h].push(r), n;
        },
        {}
      );
    }
    const Fe = x(() => we(_e, (e) => p(e.value))), Ce = ee(
      ((ie = a.items) == null ? void 0 : ie.map((e) => ({ value: e.value, collapsed: e.collapsed ?? !1 }))) ?? []
    ), X = ee(
      ((re = a.groups) == null ? void 0 : re.reduce(
        (e, t) => (e[t.id] = t.collapse ?? !1, e),
        {}
      )) ?? {}
    );
    function Y({ groupId: e, value: t }) {
      var n;
      return e && X[e] || ((n = Ce.find((r) => he(r.value, t))) == null ? void 0 : n.collapsed);
    }
    function Ve(e) {
      X[e] = !X[e];
    }
    const Z = x(() => {
      const e = /* @__PURE__ */ new Map();
      for (const t of a.items)
        if (t.groupId) {
          const n = p(t.value), r = e.get(t.groupId) ?? [];
          r.push(n), e.set(t.groupId, r);
        }
      return e;
    });
    function q(e) {
      const t = Z.value.get(e) || [], n = A.value;
      return t.length > 0 && t.every((r) => n.has(r));
    }
    function ae(e) {
      const t = Z.value.get(e) || [], n = A.value;
      return t.some((r) => n.has(r)) && !q(e);
    }
    function Ae(e) {
      const t = Z.value.get(e) || [];
      if (!t.length) return;
      const n = A.value;
      if (q(e))
        o.value = o.value.filter((r) => !t.includes(p(r)));
      else if (ae(e)) {
        const r = [];
        for (const h of a.items)
          if (h.groupId === e) {
            const _ = p(h.value);
            n.has(_) || r.push(h.value);
          }
        r.length && (o.value = [...o.value, ...r]);
      } else {
        const r = a.items.filter((h) => h.groupId === e).map((h) => h.value);
        o.value = [...o.value, ...r];
      }
    }
    const J = $(0);
    function se(e) {
      var t;
      J.value = ((t = e.target) == null ? void 0 : t.scrollTop) ?? J;
    }
    const Me = x(() => J.value > 100), R = $(), ne = $(), Te = x(() => a.maxSelections ? Math.max(0, a.maxSelections - o.value.length) : null), Be = x(() => a.maxSelections && o.value.length >= a.maxSelections);
    return (e, t) => {
      var de, ce;
      const n = T("FmIcon"), r = T("FmTextField"), h = T("FmCircularProgress"), _ = T("FmCheckbox"), M = T("FmListItem"), Le = T("FmChip"), ue = T("FmButton"), Ie = T("FmList");
      return d(), f("div", {
        class: "relative max-h-[380px] overflow-y-auto overflow-x-hidden",
        onScroll: se,
        onScrollend: se,
        ref_key: "scrollable",
        ref: R
      }, [
        B("div", qe, [
          e.noShadow ? D("", !0) : (d(), f("div", et)),
          B("div", tt, [
            B("div", lt, [
              k(n, { name: "autorenew" }),
              B("span", at, [
                Ke(P(L(l)("inventory.formField.itemsSelected", { count: ((de = o.value) == null ? void 0 : de.length) ?? 0 })) + " ", 1),
                e.maxSelections ? (d(), f("span", st, " (" + P(L(l)("inventory.formField.maximum")) + " " + P(Te.value) + ") ", 1)) : D("", !0)
              ])
            ]),
            k(r, {
              class: "basis-1/2",
              modelValue: E.value,
              "onUpdate:modelValue": t[1] || (t[1] = (s) => E.value = s),
              "prepend-icon": "search",
              placeholder: e.placeholder,
              ref_key: "searchTextField",
              ref: ne
            }, je({ _: 2 }, [
              E.value.length ? {
                name: "append",
                fn: w(() => [
                  k(n, {
                    class: "cursor-pointer",
                    name: "close",
                    onClick: t[0] || (t[0] = (s) => E.value = "")
                  })
                ]),
                key: "0"
              } : void 0
            ]), 1032, ["modelValue", "placeholder"])
          ]),
          e.filterAttributes && e.filterAttributes.length > 0 ? (d(), f("div", nt, [
            k(L(Qe), {
              modelValue: G.value,
              "onUpdate:modelValue": t[2] || (t[2] = (s) => G.value = s),
              attributes: e.filterAttributes
            }, null, 8, ["modelValue", "attributes"])
          ])) : D("", !0),
          Be.value ? (d(), f("div", ot, [
            k(n, {
              name: "warning",
              class: "text-fm-color-state-warning-base"
            }),
            B("span", it, P(L(l)("inventory.formField.maxSelectionsReached", { count: e.maxSelections })), 1)
          ])) : D("", !0)
        ]),
        B("div", null, [
          e.loading ? (d(), f("div", rt, [
            k(h, {
              size: "md",
              color: "neutral-gray-400"
            }),
            B("div", ut, P(L(l)("inventory.formField.loading")), 1)
          ])) : b.value.length ? (d(), I(Ie, { key: 1 }, {
            default: w(() => [
              e.singleSelect ? D("", !0) : (d(), I(M, {
                key: 0,
                "model-value": K.value,
                indeterminate: le.value,
                value: "",
                onClick: t[3] || (t[3] = (s) => Se())
              }, {
                prepend: w(() => [
                  k(_, {
                    "model-value": K.value,
                    indeterminate: le.value,
                    label: K.value ? L(l)("inventory.formField.select.removeAll") : L(l)("inventory.formField.select.selectAll"),
                    value: ""
                  }, null, 8, ["model-value", "indeterminate", "label"])
                ]),
                _: 1
              }, 8, ["model-value", "indeterminate"])),
              e.groups ? (d(!0), f(V, { key: 2 }, O(e.groups, (s) => (d(), f(V, {
                key: s.id
              }, [
                k(M, {
                  label: s.label,
                  sublabel: s.sublabel,
                  style: { "padding-top": "4px", "padding-bottom": "4px", "padding-left": "32px" },
                  disabled: s.disabled
                }, {
                  prepend: w(() => [
                    k(_, {
                      "model-value": q(s.id),
                      indeterminate: ae(s.id),
                      onClick: () => Ae(s.id),
                      value: s.id,
                      disabled: s.disabled
                    }, null, 8, ["model-value", "indeterminate", "onClick", "value", "disabled"])
                  ]),
                  append: w(() => [
                    k(ue, {
                      "prepend-icon": Y({ groupId: s.id }) ? "expand_less" : "expand_more",
                      size: "md",
                      variant: "tertiary",
                      onClick: Ne(() => Ve(s.id), ["stop"])
                    }, null, 8, ["prepend-icon", "onClick"])
                  ]),
                  _: 2
                }, 1032, ["label", "sublabel", "disabled"]),
                (d(!0), f(V, null, O(b.value.filter((y) => y.groupId === s.id), (y) => (d(), f("div", {
                  key: p(y.value),
                  class: fe([
                    "transition-all overflow-y-clip",
                    Y({ groupId: s.id, value: y.value }) ? "max-h-0 opacity-0" : "max-h-[64px] opacity-100"
                  ])
                }, [
                  k(M, {
                    label: y.label,
                    sublabel: y.sublabel,
                    disabled: y.disabled,
                    onClick: (j) => H(y.value, !g(y.value)),
                    style: { "padding-top": "4px", "padding-bottom": "4px", "padding-left": "64px" }
                  }, {
                    prepend: w(() => [
                      k(_, {
                        "model-value": g(y.value),
                        "onUpdate:modelValue": (j) => H(y.value, j),
                        value: y.value,
                        disabled: y.disabled
                      }, null, 8, ["model-value", "onUpdate:modelValue", "value", "disabled"])
                    ]),
                    _: 2
                  }, 1032, ["label", "sublabel", "disabled", "onClick"])
                ], 2))), 128))
              ], 64))), 128)) : (d(), f(V, { key: 1 }, [
                e.virtualScroll ? (d(), I(Ye, {
                  key: 0,
                  length: b.value.length + m.value.length,
                  "item-height": 64,
                  scrollTop: J.value,
                  prerender: 100
                }, {
                  default: w(({ getItem: s, index: y }) => {
                    var j, me;
                    return [
                      y < b.value.length ? (d(), I(M, {
                        key: 0,
                        label: s(b.value).label,
                        sublabel: s(b.value).sublabel,
                        onClick: (N) => H(
                          s(b.value).value,
                          !g(s(b.value).value)
                        ),
                        disabled: (me = (j = Fe.value[p(s(b.value).value)]) == null ? void 0 : j[0]) == null ? void 0 : me.disabled,
                        style: z({
                          paddingTop: 4,
                          paddingBottom: 4,
                          paddingLeft: e.singleSelect === !0 ? 12 : 32
                        })
                      }, {
                        prepend: w(() => [
                          k(_, {
                            "model-value": g(s(b.value).value),
                            "onUpdate:modelValue": (N) => H(s(b.value).value, N),
                            value: s(b.value).value,
                            disabled: s(b.value).disabled
                          }, null, 8, ["model-value", "onUpdate:modelValue", "value", "disabled"])
                        ]),
                        append: w(() => {
                          var N;
                          return [
                            (N = s(b.value)) != null && N.tags ? (d(), f("div", dt, [
                              (d(!0), f(V, null, O(s(b.value).tags, (De, $e) => (d(), I(Le, {
                                key: $e,
                                label: De,
                                compact: ""
                              }, null, 8, ["label"]))), 128))
                            ])) : D("", !0),
                            ke(e.$slots, "appendListItem", {
                              item: s(b.value)
                            }, void 0, !0)
                          ];
                        }),
                        _: 2
                      }, 1032, ["label", "sublabel", "onClick", "disabled", "style"])) : (d(), I(pe(m.value[y - b.value.length]), { key: 1 }))
                    ];
                  }),
                  _: 3
                }, 8, ["length", "scrollTop"])) : (d(), f(V, { key: 1 }, [
                  (d(!0), f(V, null, O(b.value, (s) => (d(), f("div", {
                    key: p(s.value),
                    class: fe([
                      "transition-all overflow-y-clip",
                      Y({ value: s.value }) ? "max-h-0 opacity-0" : "max-h-[64px] opacity-100"
                    ])
                  }, [
                    k(M, {
                      label: s.label,
                      sublabel: s.sublabel,
                      onClick: (y) => H(s.value, !g(s.value)),
                      disabled: s.disabled,
                      style: z({
                        paddingTop: 4,
                        paddingBottom: 4,
                        paddingLeft: e.singleSelect === !0 ? 12 : 32
                      })
                    }, {
                      prepend: w(() => [
                        k(_, {
                          "model-value": g(s.value),
                          "onUpdate:modelValue": (y) => H(s.value, y),
                          value: s.value,
                          disabled: s.disabled
                        }, null, 8, ["model-value", "onUpdate:modelValue", "value", "disabled"])
                      ]),
                      _: 2
                    }, 1032, ["label", "sublabel", "onClick", "disabled", "style"])
                  ], 2))), 128)),
                  (d(!0), f(V, null, O(m.value, (s, y) => (d(), f("div", { key: y }, [
                    (d(), I(pe(s)))
                  ]))), 128))
                ], 64))
              ], 64))
            ]),
            _: 3
          })) : (d(), f("div", ct, P(L(l)("inventory.formField.noItemsFound")), 1))
        ]),
        B("div", {
          class: "sticky bottom-24",
          style: z({ transform: `translateX(${(((ce = R.value) == null ? void 0 : ce.clientWidth) ?? 0) - 80}px)` })
        }, [
          k(ze, { name: "fade" }, {
            default: w(() => [
              Me.value ? (d(), I(ue, {
                key: 0,
                variant: "fab",
                class: "bg-white",
                "text-color": "neutral-gray-400",
                "prepend-icon": "keyboard_arrow_up",
                onClick: t[4] || (t[4] = () => {
                  var s;
                  return (s = R.value) == null ? void 0 : s.scrollTo({ top: 0, behavior: "smooth" });
                })
              })) : D("", !0)
            ]),
            _: 1
          })
        ], 4)
      ], 544);
    };
  }
}), St = /* @__PURE__ */ Xe(mt, [["__scopeId", "data-v-3d54ea9a"]]);
export {
  U as F,
  St as a,
  Ze as b
};
