import { ref as S, computed as o, defineComponent as ae, mergeModels as ne, useModel as ue, watch as j, resolveComponent as k, openBlock as i, createElementBlock as b, createElementVNode as C, createVNode as y, withCtx as L, normalizeClass as re, toDisplayString as oe, createCommentVNode as N, createBlock as O, withModifiers as K, unref as g, isRef as se, Fragment as Q, renderList as T, renderSlot as ie, nextTick as ce } from "vue";
import { t as ve, u as de, a as fe, O as E, b as me } from "./app-5956b5d6.js";
import "@feedmepos/ui-library";
import "pinia";
import "vue-router";
function pe(f, t, c = f) {
  const a = S(!0), v = o(() => {
    if (!a.value && !t.value)
      return !0;
    if (t.value && t.value.length > 0) {
      const u = new Set(c.value.map((n) => n.value));
      return t.value.length === c.value.length && t.value.every((n) => u.has(n));
    }
    return !1;
  }), x = o(() => {
    var u, n, h, m;
    return !!((u = t.value) != null && u.length && ((n = t.value) == null ? void 0 : n.length) > 0 && ((h = t.value) == null ? void 0 : h.length) < ((m = f.value) == null ? void 0 : m.length));
  });
  return { allCleared: a, allSelected: v, indeterminate: x, toggleAll: () => {
    if (v.value) {
      t.value = void 0, a.value = !0;
      return;
    }
    const u = c.value.map((n) => n.value);
    u.length === f.value.length ? (t.value = void 0, a.value = !1) : (t.value = u, a.value = !1);
  }, toggleOption: (u) => {
    var h, m, w;
    let n;
    t.value === void 0 && a.value ? n = [u] : t.value === void 0 && !a.value ? n = (h = f.value.map((p) => p.value)) == null ? void 0 : h.filter((p) => p !== u) : (m = t.value) != null && m.includes(u) ? n = (w = t.value) == null ? void 0 : w.filter((p) => p !== u) : n = [...t.value || [], u], n.length === 0 ? (t.value = void 0, a.value = !0) : n.length === f.value.length ? (t.value = void 0, a.value = !1) : t.value = n;
  } };
}
const ge = { class: "w-full flex flex-col gap-8" }, he = { class: "flex items-center gap-8" }, be = { class: "inline-flex items-center" }, ye = { class: "flex flex-col w-[360px] min-h-0 gap-8" }, _e = { class: "flex gap-4 px-[6px]" }, ke = { class: "flex-1" }, Ce = { class: "flex-1 overflow-y-auto min-h-0" }, Se = { key: 0 }, Me = /* @__PURE__ */ ae({
  __name: "RestaurantSelector",
  props: /* @__PURE__ */ ne({
    multiple: { type: Boolean, default: !1 },
    excludeRestaurants: {},
    managableOnly: { type: Boolean, default: !0 },
    disabled: { type: Boolean, default: !1 },
    variant: { default: "default" },
    autoSelectFirst: { type: Boolean, default: !1 }
  }, {
    modelValue: {},
    modelModifiers: {}
  }),
  emits: ["update:modelValue"],
  setup(f) {
    const t = f, { t: c } = ve(), a = ue(f, "modelValue"), { restaurants: v, restaurantAttributeSettings: x } = de(), R = o(() => v.value.length === 0);
    function P(e) {
      const l = v.value.map((_) => {
        var A;
        return (A = _.customAttributes) == null ? void 0 : A[e];
      }).filter((_) => typeof _ == "string" && !!_);
      return [...new Set(l)];
    }
    const u = o(() => x.value.map((e) => {
      const l = P(e.key);
      return {
        ...e,
        values: l
      };
    })), { searchKey: n, filter: h } = fe(), m = S(null);
    function w(e) {
      var l;
      if (t.multiple)
        throw new Error("Single selection is not supported for multiple selection");
      a.value = e, (l = m.value) == null || l.toggleMenu(!1);
    }
    const p = (e) => {
      const { name: l, code: r } = e.profile;
      return {
        label: `${l} (${r ?? "--"})`,
        value: e._id
      };
    }, M = o(() => v.value.map(p)), F = o(
      () => v.value.filter((e) => t.managableOnly ? e.managable : !0).filter((e) => t.multiple ? !0 : E(e.customAttributes || {}, d.value)).map(p).filter(({ label: e }) => h([e])).filter((e) => {
        var l;
        return !((l = t.excludeRestaurants) != null && l.includes(e.value));
      })
    ), W = o(() => {
      var e;
      return a.value ? a.value.length === 1 ? (e = M.value.find((l) => {
        var r;
        return l.value === ((r = a.value) == null ? void 0 : r[0]);
      })) == null ? void 0 : e.label : c("portalshell.components.restaurantSelector.restaurantCount", {
        count: a.value.length
      }) : c("portalshell.components.restaurantSelector.allRestaurants");
    }), q = o(() => {
      var e;
      return a.value ? ((e = M.value.find((l) => l.value === a.value)) == null ? void 0 : e.label) || c("portalshell.components.restaurantSelector.unknownRestaurant") : c("portalshell.components.restaurantSelector.selectRestaurant");
    }), D = o(() => t.multiple ? W.value : q.value), B = o(() => t.disabled ? "neutral-gray-200" : t.variant === "dark" ? "neutral-black" : "neutral-gray-400"), G = o(() => t.disabled ? "text-fm-color-neutral-gray-200" : t.variant === "dark" ? "text-fm-color-neutral-black" : "text-fm-color-neutral-gray-400"), { allSelected: U, toggleAll: H, toggleOption: J, indeterminate: X } = pe(
      M,
      a,
      F
    ), V = S(!1), Y = o(() => x.value.length > 0), z = S(null);
    async function Z() {
      t.disabled || (V.value ? V.value = !1 : (V.value = !0, await ce(), z.value && z.value.toggleProperty(!0)));
    }
    const d = S((() => ({
      logic: "AND",
      rules: []
    }))()), ee = o(() => {
      var e, l;
      return ((l = (e = d.value) == null ? void 0 : e.rules) == null ? void 0 : l.length) || 0;
    });
    return j(
      d,
      () => {
        var e;
        if (t.multiple) {
          if (!d.value || d.value.rules.length === 0) {
            a.value = null;
            return;
          }
          const l = v.value.filter((r) => E(r.customAttributes || {}, d.value));
          a.value = l.map((r) => r._id);
        } else {
          const l = v.value.filter((r) => E(r.customAttributes || {}, d.value));
          l.length === 0 ? a.value = null : a.value = (e = l[0]) == null ? void 0 : e._id;
        }
      },
      {
        deep: !0
      }
    ), j(
      [() => t.autoSelectFirst, F],
      ([e, l]) => {
        e && l.length > 0 && !a.value && (a.value = l[0].value);
      },
      { immediate: !0 }
    ), (e, l) => {
      const r = k("FmIcon"), _ = k("FmCircularProgress"), A = k("FmChip"), le = k("FmSearch"), $ = k("FmMenuItem"), te = k("FmMenu");
      return i(), b("div", ge, [
        C("div", he, [
          y(te, {
            "close-on-click": !1,
            disabled: e.disabled || R.value,
            ref_key: "menuRef",
            ref: m,
            "z-index": 50,
            "selector-for-dismiss-away-event-exclusion": ".fm-dialog, .fm-select__menu__wrapper"
          }, {
            "menu-button": L(() => [
              C("div", be, [
                y(A, { disabled: e.disabled }, {
                  default: L(() => [
                    y(r, {
                      color: B.value,
                      name: "storefront",
                      outline: "",
                      size: "sm"
                    }, null, 8, ["color"]),
                    D.value ? (i(), b("div", {
                      key: 0,
                      class: re([
                        "fm-typo-en-body-md-600 select-none text-ellipsis overflow-hidden whitespace-nowrap",
                        G.value
                      ])
                    }, oe(D.value), 3)) : N("", !0),
                    R.value ? (i(), O(_, {
                      key: 1,
                      size: "md"
                    })) : (i(), O(r, {
                      key: 2,
                      color: B.value,
                      name: "keyboard_arrow_down",
                      size: "sm"
                    }, null, 8, ["color"])),
                    Y.value ? (i(), b("div", {
                      key: 3,
                      class: "pl-8 flex items-center justify-center border-l border-fm-color-neutral-gray-200",
                      onClick: K(Z, ["stop"])
                    }, [
                      y(r, {
                        name: "filter_alt",
                        size: "sm",
                        outline: ee.value === 0,
                        color: B.value,
                        onClick: l[0] || (l[0] = K(() => {
                        }, ["prevent"]))
                      }, null, 8, ["outline", "color"])
                    ])) : N("", !0)
                  ]),
                  _: 1
                }, 8, ["disabled"])
              ])
            ]),
            default: L(() => [
              C("div", ye, [
                C("div", _e, [
                  C("div", ke, [
                    y(le, {
                      modelValue: g(n),
                      "onUpdate:modelValue": l[1] || (l[1] = (s) => se(n) ? n.value = s : null),
                      placeholder: g(c)("common.search")
                    }, null, 8, ["modelValue", "placeholder"])
                  ])
                ]),
                C("div", Ce, [
                  e.multiple ? (i(), b(Q, { key: 0 }, [
                    y($, {
                      label: "All",
                      "has-checkbox": "",
                      "model-value": g(U),
                      indeterminate: g(X),
                      onClick: g(H)
                    }, null, 8, ["model-value", "indeterminate", "onClick"]),
                    (i(!0), b(Q, null, T(F.value, (s) => {
                      var I;
                      return i(), O($, {
                        key: s.value,
                        label: s.label,
                        "has-checkbox": "",
                        "model-value": g(U) || ((I = a.value) == null ? void 0 : I.includes(s.value)),
                        onClick: (we) => g(J)(s.value)
                      }, null, 8, ["label", "model-value", "onClick"]);
                    }), 128))
                  ], 64)) : (i(!0), b(Q, { key: 1 }, T(F.value, (s) => (i(), O($, {
                    key: s.value,
                    label: s.label,
                    onClick: (I) => w(s.value)
                  }, null, 8, ["label", "onClick"]))), 128))
                ])
              ])
            ]),
            _: 1
          }, 8, ["disabled"]),
          ie(e.$slots, "append-right")
        ]),
        V.value ? (i(), b("div", Se, [
          y(g(me), {
            ref_key: "filterRef",
            ref: z,
            attributes: u.value,
            modelValue: d.value,
            "onUpdate:modelValue": l[2] || (l[2] = (s) => d.value = s)
          }, null, 8, ["attributes", "modelValue"])
        ])) : N("", !0)
      ]);
    };
  }
});
export {
  Me as default
};
