import { openBlock as c, createElementBlock as d, createElementVNode as z, ref as v, computed as p, onMounted as Le, watch as Te, onBeforeUnmount as $e, resolveComponent as le, resolveDirective as Me, createBlock as S, unref as u, withCtx as y, Fragment as K, renderList as U, withDirectives as re, normalizeStyle as L, createCommentVNode as b, isRef as qe, withKeys as Oe, renderSlot as T, normalizeClass as B, resolveDynamicComponent as Ae, createVNode as Ve } from "vue";
import pe from "axios";
import { isPlainObject as g, difference as Ne, differenceBy as Pe, isEqual as Ee, isArray as C, set as ce, isNumber as He, isFunction as Re } from "lodash-es";
import { a6 as Ke, a7 as Ue, a8 as Y, a9 as Be, aa as Ye, ab as We, P as je, ac as De, ad as Fe, ae as ue, af as Ge, ag as Je, ah as Qe } from "./index-1e3e8095.js";
import { e as se, t as Xe } from "./itemSelectOptions-01e8c122.js";
import "@vueuse/core";
import "vue-i18n";
const Ze = {
  viewBox: "0 0 1536 1536",
  width: "1em",
  height: "1em"
}, et = /* @__PURE__ */ z("path", {
  d: "M1149 994q0-26-19-45L949 768l181-181q19-19 19-45q0-27-19-46l-90-90q-19-19-46-19q-26 0-45 19L768 587L587 406q-19-19-45-19q-27 0-46 19l-90 90q-19 19-19 46q0 26 19 45l181 181l-181 181q-19 19-19 45q0 27 19 46l90 90q19 19 46 19q26 0 45-19l181-181l181 181q19 19 45 19q27 0 46-19l90-90q19-19 19-46zm387-226q0 209-103 385.5T1153.5 1433T768 1536t-385.5-103T103 1153.5T0 768t103-385.5T382.5 103T768 0t385.5 103T1433 382.5T1536 768z",
  fill: "currentColor"
}, null, -1), tt = [
  et
];
function ot(W, j) {
  return c(), d("svg", Ze, [...tt]);
}
const nt = { name: "fa-times-circle", render: ot }, it = `.kapix-autocomplete{width:100%;height:100%;min-width:0;min-height:0;display:flex;flex-wrap:wrap}.kapix-autocomplete-search-input{height:auto;width:auto;flex:1;align-self:center;margin-left:5px}.kapix-autocomplete-chips-container{width:100%;height:100%;min-width:0;min-height:0;display:flex;flex-direction:row;flex-wrap:wrap;align-items:center}.kapix-autocomplete-chips-container.display-only{align-self:baseline;max-height:100%;height:auto;overflow-x:hidden;overflow-y:auto}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-thumb{background-color:currentColor;border-radius:9px;border:solid 6px transparent;background-clip:content-box}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-thumb:hover{border-width:5px}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar{height:18px;width:18px}@media (max-width: 599.98px){.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar{height:0;width:0}}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar:disabled{display:none}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-button,.kapix-autocomplete-chips-container.display-only ::-webkit-scrollbar-corner{cursor:pointer;background:transparent}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-button:horizontal:decrement,.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-button:horizontal:increment,.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-button:vertical:decrement,.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-button:vertical:increment{display:none}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-button:vertical:start:increment,.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-button:vertical:end:decrement,.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-button:horizontal:start:increment,.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-button:horizontal:end:decrement{display:none}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-track{background:transparent}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-thumb:vertical{min-height:50px}.kapix-autocomplete-chips-container.display-only::-webkit-scrollbar-thumb:horizontal{min-width:50px}.kapix-autocomplete-chips-container .kapix-generated-chip{margin:1px;color:#000}.kapix-autocomplete-chips-container .kapix-chip-icon{margin-left:6px;margin-right:-6px;font-size:14px;height:14px;width:14px}.kapix-autocomplete-input-menu-container{display:flex;flex-direction:column;text-align:left;border-radius:5px;min-width:100px;box-shadow:0 3px 3px -2px #0003,0 3px 4px #00000024,0 1px 8px #0000001f}.kapix-autocomplete-input-menu-container::-webkit-scrollbar-thumb{background-color:currentColor;border-radius:9px;border:solid 6px transparent;background-clip:content-box}.kapix-autocomplete-input-menu-container::-webkit-scrollbar-thumb:hover{border-width:5px}.kapix-autocomplete-input-menu-container::-webkit-scrollbar{height:18px;width:18px}@media (max-width: 599.98px){.kapix-autocomplete-input-menu-container::-webkit-scrollbar{height:0;width:0}}.kapix-autocomplete-input-menu-container::-webkit-scrollbar:disabled{display:none}.kapix-autocomplete-input-menu-container::-webkit-scrollbar-button,.kapix-autocomplete-input-menu-container ::-webkit-scrollbar-corner{cursor:pointer;background:transparent}.kapix-autocomplete-input-menu-container::-webkit-scrollbar-button:horizontal:decrement,.kapix-autocomplete-input-menu-container::-webkit-scrollbar-button:horizontal:increment,.kapix-autocomplete-input-menu-container::-webkit-scrollbar-button:vertical:decrement,.kapix-autocomplete-input-menu-container::-webkit-scrollbar-button:vertical:increment{display:none}.kapix-autocomplete-input-menu-container::-webkit-scrollbar-button:vertical:start:increment,.kapix-autocomplete-input-menu-container::-webkit-scrollbar-button:vertical:end:decrement,.kapix-autocomplete-input-menu-container::-webkit-scrollbar-button:horizontal:start:increment,.kapix-autocomplete-input-menu-container::-webkit-scrollbar-button:horizontal:end:decrement{display:none}.kapix-autocomplete-input-menu-container::-webkit-scrollbar-track{background:transparent}.kapix-autocomplete-input-menu-container::-webkit-scrollbar-thumb:vertical{min-height:50px}.kapix-autocomplete-input-menu-container::-webkit-scrollbar-thumb:horizontal{min-width:50px}.kapix-autocomplete-input-menu-container .kapix-autocomplete-input-item{white-space:nowrap;position:relative;overflow:hidden;text-overflow:ellipsis;display:flex;flex-direction:row;min-height:30px;padding:5px 10px;justify-content:space-between;width:100%;align-items:center}.kapix-autocomplete-input-menu-container .kapix-autocomplete-input-item .icon{margin-right:5px;width:30px}.kapix-autocomplete-input-menu-container .kapix-autocomplete-input-item .label{text-align:left;width:100%}.kapix-autocomplete-input-menu-container .kapix-autocomplete-input-item .subtitle{white-space:break-spaces;margin-top:5px;margin-bottom:5px;font-size:11px;opacity:.7}.kapix-autocomplete-input-menu-container .kapix-autocomplete-input-item.subtitle{padding:10px}.kapix-autocomplete-input-menu-container .kapix-autocomplete-input-item.disabled{background-color:#d3d3d34d;opacity:.5;cursor:no-drop}.kapix-autocomplete-input-menu-container .kapix-autocomplete-input-item:hover{background-color:#d3d3d34d}.kapix-autocomplete-input-menu{overflow-x:hidden;overflow-y:auto;max-height:40vh}.kapix-autocomplete-input-menu::-webkit-scrollbar-thumb{background-color:currentColor;border-radius:9px;border:solid 6px transparent;background-clip:content-box}.kapix-autocomplete-input-menu::-webkit-scrollbar-thumb:hover{border-width:5px}.kapix-autocomplete-input-menu::-webkit-scrollbar{height:18px;width:18px}@media (max-width: 599.98px){.kapix-autocomplete-input-menu::-webkit-scrollbar{height:0;width:0}}.kapix-autocomplete-input-menu::-webkit-scrollbar:disabled{display:none}.kapix-autocomplete-input-menu::-webkit-scrollbar-button,.kapix-autocomplete-input-menu ::-webkit-scrollbar-corner{cursor:pointer;background:transparent}.kapix-autocomplete-input-menu::-webkit-scrollbar-button:horizontal:decrement,.kapix-autocomplete-input-menu::-webkit-scrollbar-button:horizontal:increment,.kapix-autocomplete-input-menu::-webkit-scrollbar-button:vertical:decrement,.kapix-autocomplete-input-menu::-webkit-scrollbar-button:vertical:increment{display:none}.kapix-autocomplete-input-menu::-webkit-scrollbar-button:vertical:start:increment,.kapix-autocomplete-input-menu::-webkit-scrollbar-button:vertical:end:decrement,.kapix-autocomplete-input-menu::-webkit-scrollbar-button:horizontal:start:increment,.kapix-autocomplete-input-menu::-webkit-scrollbar-button:horizontal:end:decrement{display:none}.kapix-autocomplete-input-menu::-webkit-scrollbar-track{background:transparent}.kapix-autocomplete-input-menu::-webkit-scrollbar-thumb:vertical{min-height:50px}.kapix-autocomplete-input-menu::-webkit-scrollbar-thumb:horizontal{min-width:50px}
`, $ = Object.freeze({
  NORMAL: null,
  SEARCH_ONLY: 1,
  CHIPS_ONLY: 2
}), at = { class: "kapix-autocomplete-chips-container" }, lt = {
  ref: "scrollerMenu",
  class: "kapix-autocomplete-input-menu"
}, rt = ["data-cy", "onClick"], pt = {
  key: 2,
  class: "text-no-wrap label"
}, ct = ["innerHTML"], ut = ["innerHTML"], st = {
  key: 1,
  class: "kapix-autocomplete-chips-container display-only"
}, dt = 200, wt = {
  __name: "KapixAutoCompleteInput",
  props: Ke(Array),
  emits: ["update:modelValue", "update:search"],
  setup(W, { emit: j }) {
    Ue("KapixAutoCompleteInput", it);
    const M = j, a = W, D = v(null), q = v(!1), _ = v(!1), O = v(null), de = v(0), A = v(null), V = v(null), me = p(() => `search-${de.value}`), be = p(() => a.options.itemText ?? "label"), he = p(() => a.options.itemValue ?? "value"), h = p(() => {
      var e;
      return ((e = a.options.dataSource) == null ? void 0 : e.itemText) ?? "label";
    }), w = p(() => {
      var e;
      return ((e = a.options.dataSource) == null ? void 0 : e.itemValue) ?? "value";
    });
    function F(e) {
      if (!g(e)) {
        console.error("toMenuItemOptions accept only plainObject");
        return;
      }
      return Xe(e, {
        ...a.options,
        ...a.options.dataSource
      });
    }
    function N(e) {
      const t = F(e);
      return G(t);
    }
    function G(e) {
      var t, n;
      if (e) {
        const { chipColor: l, chipBackground: i } = a.options;
        l && !((t = e.style) != null && t.color) && ce(e, "style.color", l), i && !((n = e.style) != null && n.background) && ce(e, "style.background", De(i));
      }
      return e;
    }
    function ke(e) {
      return C(e) ? e.filter((t) => !Fe(t)).map((t) => g(t) ? F({
        ...t,
        [h.value]: t[be.value],
        [w.value]: t[he.value]
      }) : {
        [h.value]: t,
        [w.value]: t
      }) : He(e) ? new Array(Math.min(2e3, e)).fill(0).map((t, n) => ({
        [h.value]: n,
        [w.value]: n
      })) : ue;
    }
    function xe(e) {
      return C(e) ? e : ue;
    }
    const J = p(() => ke(a.options.items)), f = v(null), P = p(() => f.value ? [...f.value, ...J.value] : J.value), ve = p(() => a.options.menuPlacement || "bottom-start"), ye = p(() => Y(a.options.distinct, !0, !1)), Q = p(() => Y(a.options.sensitiveCase, !0, !1)), X = p(() => Y(a.options.deletableChips, !0, !1)), I = p(() => a.options.autoCompleteDisplayMode), m = p({
      get() {
        return xe(Be(a));
      },
      set(e) {
        var t;
        M("update:modelValue", e), (t = a.onValueChanged) == null || t.call(a, e);
      }
    }), Z = p(() => m.value.map((e) => {
      const t = P.value.find((n) => e === (n == null ? void 0 : n[w.value]) || e === n);
      return g(t) ? N(t) : g(e) ? N(e) : G({
        label: e,
        value: e
      });
    }));
    function ee(e) {
      var t;
      O.value = e, M("update:search", e), (t = V.value) == null || t.updatePosition();
    }
    const k = p({
      get() {
        return O.value;
      },
      set(e) {
        ee(e), H();
      }
    }), x = p(() => {
      var e;
      return (e = k.value) == null ? void 0 : e.toLowerCase();
    });
    function E(e, t) {
      return e != null && e.toString().toLowerCase().includes(t);
    }
    const we = p(() => {
      const { allowUserInput: e } = a.options;
      let t = [];
      const n = {};
      P.value.every((i) => {
        if (t.length >= dt)
          return !1;
        if (g(i)) {
          const s = N(i), o = s.label, r = s.value;
          n[r] = s, x.value ? (E(r, x.value) || E(o, x.value)) && t.push(r) : t.push(r);
        } else
          n[i] = {
            label: i,
            value: i
          }, x.value ? E(i, x.value) && t.push(i) : t.push(i);
        return !0;
      }), e && x.value && !ge(t) && (t = [...t, x.value]), ye.value && (Q.value ? t = Ne(t, m.value) : t = Pe(t.map((i) => ({
        lower: i && i.toString().toLowerCase(),
        original: i
      })), m.value.map((i) => ({
        lower: i && i.toString().toLowerCase(),
        original: i
      })), "lower").map((i) => i.original));
      const l = t.length - 1;
      return t.map((i, s) => {
        const o = e && s === l && n[i] == null, r = n[i];
        return {
          ...r,
          isUserInput: o,
          label: r ? r[h.value] : i,
          value: i
        };
      });
    }), fe = p(() => A.value && { minWidth: `${A.value}px` });
    function ge(e) {
      return Q.value ? e.includes(k.value) : e.some((t) => t.toLowerCase() === x.value.toLowerCase());
    }
    function Ce(e) {
      return C(e) ? e[0] : e;
    }
    function te(e, t) {
      const n = e && Ce(e.data);
      if (n) {
        const l = n[w.value], i = n[h.value];
        l && (t.value = l), i && (t.label = i), l && i && (f.value = [
          ...f.value,
          {
            [h.value]: i,
            [w.value]: l
          }
        ], a.options.dataSource.getAll && Qe(a.options.dataSource.getAll.url));
      }
    }
    async function _e(e) {
      if (e && e.isUserInput && a.options.dataSource) {
        const t = a.options.dataSource.post || a.options.dataSource.getAll;
        if (Re(t))
          te(await t({
            [h.value]: e.value
          }), e);
        else if (t && t.url) {
          const n = se(t);
          te(await pe.post(t.url, {
            ...t.data,
            [h.value]: e.value
          }, n), e);
        }
      }
    }
    async function oe(e) {
      const t = _e(e), n = [
        ...m.value
      ];
      ie(e.value, n), ne();
      try {
        Ge(t) && (await t, ie(e.value, n)), ee(null);
      } catch {
        m.value = n, H();
      }
    }
    function H() {
      var e, t;
      I.value !== $.CHIPS_ONLY && (((e = P.value) == null ? void 0 : e.length) > 0 || a.options.allowUserInput && k.value) && (_.value = !0, A.value = (t = V.value.$el) == null ? void 0 : t.offsetWidth);
    }
    function ne() {
      _.value = !1, O.value = null;
    }
    function Se(e) {
      Je(e.target, D.value) || ne();
    }
    function ze() {
      a.options.allowUserInput && k.value && oe({
        isUserInput: !0,
        value: k.value
      });
    }
    function ie(e, t) {
      const n = t || m.value;
      if (a.options.maxSelectedItems)
        if (a.options.maxSelectedItems > n.length)
          m.value = [
            ...n,
            e
          ];
        else {
          const l = [
            ...n
          ];
          l.pop(), l.push(e), m.value = l;
        }
      else
        a.options.maxSelectedItems == null && (m.value = [
          ...n,
          e
        ]);
    }
    function Ie(e) {
      const t = [...m.value];
      t.splice(e, 1), M("update:modelValue", t);
    }
    let R;
    return Le(() => {
      Te(() => a.options.dataSource && a.options.dataSource.getAll, async (e, t) => {
        if (q.value || Ee(e, t))
          return;
        let n = !1;
        if (e)
          try {
            q.value = !0;
            const { url: l } = e, i = se(e), s = l && await Ye(l, () => pe.get(l, i));
            if (s && s.data) {
              let o;
              C(s.data) ? o = s.data : g(s.data) && forOwn(s.data, (r, ae) => {
                C(r) && (o = s.data);
              }), C(o) && (n = !0, f.value = o);
            }
          } catch (l) {
            console.error(l);
          } finally {
            q.value = !1;
          }
        n || (f.value = null);
      }, { immediate: !0 }), R = [
        We(window, "click", Se, !0)
      ];
    }), $e(() => {
      R && R.forEach((e) => e && e());
    }), (e, t) => {
      const n = nt, l = le("KapixChip"), i = le("tippy"), s = Me("tippy");
      return e.editable ? (c(), S(i, {
        key: 0,
        ref_key: "tippyInstance",
        ref: V,
        class: "kapix-autocomplete",
        visible: u(_),
        interactive: "",
        "max-width": "100%",
        "hide-on-click": !1,
        placement: u(ve),
        theme: "light-border",
        trigger: "manual",
        "data-cy": e.inputCy,
        onHidden: t[1] || (t[1] = (o) => _.value = !1),
        onClick: t[2] || (t[2] = (o) => H())
      }, {
        trigger: y(() => [
          z("div", at, [
            u(I) !== u($).SEARCH_ONLY ? (c(!0), d(K, { key: 0 }, U(u(Z), (o, r) => (c(), d("div", {
              key: r,
              class: "kapix-generated-chip"
            }, [
              re((c(), S(l, {
                close: u(X),
                style: L(e.options.noSelectionStyle ? void 0 : o.style),
                item: o
              }, {
                close: y(() => [
                  u(X) ? (c(), S(n, {
                    key: 0,
                    "data-cy": e.inputCy && `${e.inputCy}-chip-icon-${r}`,
                    class: "kapix-chip-icon",
                    onClick: (ae) => Ie(r)
                  }, null, 8, ["data-cy", "onClick"])) : b("", !0)
                ]),
                _: 2
              }, 1032, ["close", "style", "item"])), [
                [s, o.tooltip || o.description && { content: o.description }]
              ])
            ]))), 128)) : b("", !0),
            u(I) !== u($).CHIPS_ONLY ? (c(), S(je, {
              key: u(me),
              modelValue: u(k),
              "onUpdate:modelValue": t[0] || (t[0] = (o) => qe(k) ? k.value = o : null),
              "data-cy": e.inputCy && `${e.inputCy}-search`,
              class: "kapix-autocomplete-search-input",
              options: {
                ...e.options,
                rules: void 0,
                required: !1
              },
              onKeypress: Oe(ze, ["enter"])
            }, {
              "prepend-icon": y(() => [
                T(e.$slots, "prepend-icon")
              ]),
              "prepend-inner-icon": y(() => [
                T(e.$slots, "prepend-inner-icon")
              ]),
              "append-icon": y(() => [
                T(e.$slots, "append-icon")
              ]),
              "append-outer-icon": y(() => [
                T(e.$slots, "append-outer-icon")
              ]),
              _: 3
            }, 8, ["modelValue", "data-cy", "options"])) : b("", !0)
          ])
        ]),
        default: y(() => [
          u(_) && u(I) !== u($).CHIPS_ONLY ? (c(), d("div", {
            key: 0,
            ref_key: "menu",
            ref: D,
            class: "kapix-autocomplete-input-menu-container",
            style: L(u(fe))
          }, [
            z("div", lt, [
              (c(!0), d(K, null, U(u(we), (o, r) => (c(), d("div", { key: r }, [
                z("div", {
                  class: B(["kapix-autocomplete-input-item", {
                    subtitle: !!o.subtitle,
                    disabled: o.disabled
                  }]),
                  "data-cy": e.inputCy && `${e.inputCy}-item-${r}`,
                  style: L(o.style),
                  onClick: (ae) => oe(o)
                }, [
                  o.prependComponent ? (c(), S(Ae(o.prependComponent), {
                    key: 0,
                    item: o.item,
                    "formatted-item": o,
                    class: "mr-2"
                  }, null, 8, ["item", "formatted-item"])) : b("", !0),
                  o.prependIcon ? (c(), d("div", {
                    key: 1,
                    class: B([o.prependIcon, "mr-2"])
                  }, null, 2)) : b("", !0),
                  o.label ? (c(), d("div", pt, [
                    z("div", {
                      innerHTML: o.label
                    }, null, 8, ct),
                    o.subtitle ? (c(), d("div", {
                      key: 0,
                      class: "subtitle",
                      innerHTML: o.subtitle
                    }, null, 8, ut)) : b("", !0)
                  ])) : b("", !0),
                  o.icon ? (c(), d("div", {
                    key: 3,
                    class: B([o.icon, "ml-2"])
                  }, null, 2)) : b("", !0)
                ], 14, rt)
              ]))), 128))
            ], 512)
          ], 4)) : b("", !0)
        ]),
        _: 3
      }, 8, ["visible", "placement", "data-cy"])) : (c(), d("div", st, [
        (c(!0), d(K, null, U(u(Z), (o, r) => (c(), d("div", {
          key: r,
          class: "kapix-generated-chip"
        }, [
          re(Ve(l, {
            style: L(o.style),
            item: o
          }, null, 8, ["style", "item"]), [
            [s, o.tooltip || o.description && { content: o.description }]
          ])
        ]))), 128))
      ]));
    };
  }
};
export {
  wt as default
};
