import { openBlock as s, createElementBlock as h, createElementVNode as I, ref as v, computed as r, onMounted as _e, watch as Se, onBeforeUnmount as ze, resolveComponent as ae, resolveDirective as Le, createBlock as S, unref as u, withCtx as f, Fragment as R, renderList as H, withDirectives as ne, normalizeStyle as K, createCommentVNode as z, isRef as Te, withKeys as $e, renderSlot as L, createVNode as qe } from "vue";
import ie from "axios";
import { isPlainObject as C, difference as Ae, differenceBy as Ve, isEqual as Me, isArray as y, isNumber as Oe, isFunction as Ne } from "lodash-es";
import { a6 as Pe, a7 as Ee, a8 as U, a9 as Re, aa as He, ab as Ke, P as Ue, ac as Be, ad as le, ae as Ye, af as re, ag as We, ah as Fe } from "./index-9ceb1eb9.js";
import { e as ue } from "./externalApi-99353389.js";
import "@vueuse/core";
import "vue-i18n";
const De = {
  viewBox: "0 0 1536 1536",
  width: "1em",
  height: "1em"
}, je = /* @__PURE__ */ I("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), Ge = [
  je
];
function Je(B, Y) {
  return s(), h("svg", De, [...Ge]);
}
const Qe = { name: "fa-times-circle", render: Je }, Xe = `.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{display:flex;align-items:center;min-height:30px;padding:5px 10px}.kapix-autocomplete-input-menu-container .kapix-autocomplete-input-item>.kapix-autocomplete-input-item-content{display:flex;flex-direction:row;gap:5px}.kapix-autocomplete-input-menu-container .kapix-autocomplete-input-item:hover{background:rgb(214,214,214);cursor:pointer}.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}
`, T = Object.freeze({
  NORMAL: null,
  SEARCH_ONLY: 1,
  CHIPS_ONLY: 2
}), Ze = { class: "kapix-autocomplete-chips-container" }, et = {
  ref: "scrollerMenu",
  class: "kapix-autocomplete-input-menu"
}, tt = ["data-cy", "onClick"], ot = ["innerHTML"], at = {
  key: 1,
  class: "kapix-autocomplete-chips-container display-only"
}, nt = 200, mt = {
  __name: "KapixAutoCompleteInput",
  props: Pe(Array),
  emits: ["update:modelValue", "update:search"],
  setup(B, { emit: Y }) {
    Ee("KapixAutoCompleteInput", Xe);
    const $ = Y, n = B, W = v(null), q = v(!1), g = v(!1), A = v(null), ce = v(0), V = v(null), M = v(null), pe = r(() => `search-${ce.value}`), se = r(() => n.options.itemText ?? "label"), me = r(() => n.options.itemValue ?? "value"), m = r(() => {
      var e;
      return ((e = n.options.dataSource) == null ? void 0 : e.itemText) ?? "label";
    }), b = r(() => {
      var e;
      return ((e = n.options.dataSource) == null ? void 0 : e.itemValue) ?? "value";
    });
    function de(e) {
      return y(e) ? e.filter((t) => !Be(t)).map((t) => C(t) ? {
        [m.value]: t[se.value],
        [b.value]: t[me.value]
      } : {
        [m.value]: t,
        [b.value]: t
      }) : Oe(e) ? new Array(Math.min(2e3, e)).fill(0).map((t, a) => ({
        [m.value]: a,
        [b.value]: a
      })) : le;
    }
    function be(e) {
      return y(e) ? e : le;
    }
    const F = r(() => de(n.options.items)), w = v(null), O = r(() => w.value ? [...w.value, ...F.value] : F.value), he = r(() => n.options.menuPlacement || "bottom-start"), ke = r(() => U(n.options.distinct, !0, !1)), D = r(() => U(n.options.sensitiveCase, !0, !1)), j = r(() => U(n.options.deletableChips, !0, !1)), _ = r(() => n.options.autoCompleteDisplayMode), d = r({
      get() {
        return be(Re(n));
      },
      set(e) {
        var t;
        $("update:modelValue", e), (t = n.onValueChanged) == null || t.call(n, e);
      }
    }), G = r(() => d.value.map((e) => {
      const t = O.value.find((a) => e === (a && a[b.value]) || e === a);
      return C(t) ? {
        label: t[m.value],
        value: t[b.value]
      } : C(e) ? {
        label: e[m.value],
        value: e[b.value]
      } : {
        label: e,
        value: e
      };
    }));
    function J(e) {
      var t;
      A.value = e, $("update:search", e), (t = M.value) == null || t.updatePosition();
    }
    const k = r({
      get() {
        return A.value;
      },
      set(e) {
        J(e), P();
      }
    }), x = r(() => {
      var e;
      return (e = k.value) == null ? void 0 : e.toLowerCase();
    });
    function N(e, t) {
      return e != null && e.toString().toLowerCase().includes(t);
    }
    const xe = r(() => {
      const { allowUserInput: e } = n.options;
      let t = [];
      const a = {};
      O.value.every((o) => {
        if (t.length >= nt)
          return !1;
        if (C(o)) {
          const c = o[m.value], i = o[b.value];
          a[i] = o, x.value ? (N(i, x.value) || N(c, x.value)) && t.push(i) : t.push(i);
        } else
          a[o] = {
            label: o,
            value: o
          }, x.value ? N(o, x.value) && t.push(o) : t.push(o);
        return !0;
      }), e && x.value && !fe(t) && (t = [...t, x.value]), ke.value && (D.value ? t = Ae(t, d.value) : t = Ve(t.map((o) => ({
        lower: o && o.toString().toLowerCase(),
        original: o
      })), d.value.map((o) => ({
        lower: o && o.toString().toLowerCase(),
        original: o
      })), "lower").map((o) => o.original));
      const l = t.length - 1;
      return t.map((o, c) => {
        const i = e && c === l && a[o] == null, p = a[o];
        return {
          ...p,
          isUserInput: i,
          label: p ? p[m.value] : o,
          value: o
        };
      });
    }), ve = r(() => V.value && { minWidth: `${V.value}px` });
    function fe(e) {
      return D.value ? e.includes(k.value) : e.some((t) => t.toLowerCase() === x.value.toLowerCase());
    }
    function we(e) {
      return y(e) ? e[0] : e;
    }
    function Q(e, t) {
      const a = e && we(e.data);
      if (a) {
        const l = a[b.value], o = a[m.value];
        l && (t.value = l), o && (t.label = o), l && o && (w.value = [
          ...w.value,
          {
            [m.value]: o,
            [b.value]: l
          }
        ], n.options.dataSource.getAll && Fe(n.options.dataSource.getAll.url));
      }
    }
    async function ye(e) {
      if (e && e.isUserInput && n.options.dataSource) {
        const t = n.options.dataSource.post || n.options.dataSource.getAll;
        if (Ne(t))
          Q(await t({
            [m.value]: e.value
          }), e);
        else if (t && t.url) {
          const a = ue(t);
          Q(await ie.post(t.url, {
            ...t.data,
            [m.value]: e.value
          }, a), e);
        }
      }
    }
    async function X(e) {
      const t = ye(e), a = [
        ...d.value
      ];
      ee(e.value, a), Z();
      try {
        Ye(t) && (await t, ee(e.value, a)), J(null);
      } catch {
        d.value = a, P();
      }
    }
    function P() {
      var e, t;
      _.value !== T.CHIPS_ONLY && (((e = O.value) == null ? void 0 : e.length) > 0 || n.options.allowUserInput && k.value) && (g.value = !0, V.value = (t = M.value.$el) == null ? void 0 : t.offsetWidth);
    }
    function Z() {
      g.value = !1, A.value = null;
    }
    function ge(e) {
      We(e.target, W.value) || Z();
    }
    function Ce() {
      n.options.allowUserInput && k.value && X({
        isUserInput: !0,
        value: k.value
      });
    }
    function ee(e, t) {
      const a = t || d.value;
      if (n.options.maxSelectedItems)
        if (n.options.maxSelectedItems > a.length)
          d.value = [
            ...a,
            e
          ];
        else {
          const l = [
            ...a
          ];
          l.pop(), l.push(e), d.value = l;
        }
      else
        n.options.maxSelectedItems == null && (d.value = [
          ...a,
          e
        ]);
    }
    function Ie(e) {
      const t = [...d.value];
      t.splice(e, 1), $("update:modelValue", t);
    }
    function te(e) {
      return {
        color: e.color || n.options.chipColor,
        background: re(e.background) || re(n.options.chipBackground)
      };
    }
    let E;
    return _e(() => {
      Se(() => n.options.dataSource && n.options.dataSource.getAll, async (e, t) => {
        if (q.value || Me(e, t))
          return;
        let a = !1;
        if (e)
          try {
            q.value = !0;
            const { url: l } = e, o = ue(e), c = l && await He(l, () => ie.get(l, o));
            if (c && c.data) {
              let i;
              y(c.data) ? i = c.data : C(c.data) && forOwn(c.data, (p, oe) => {
                y(p) && (i = c.data);
              }), y(i) && (a = !0, w.value = i);
            }
          } catch (l) {
            console.error(l);
          } finally {
            q.value = !1;
          }
        a || (w.value = null);
      }, { immediate: !0 }), E = [
        Ke(window, "click", ge, !0)
      ];
    }), ze(() => {
      E && E.forEach((e) => e && e());
    }), (e, t) => {
      const a = Qe, l = ae("KapixChip"), o = ae("tippy"), c = Le("tippy");
      return e.editable ? (s(), S(o, {
        key: 0,
        ref_key: "tippyInstance",
        ref: M,
        class: "kapix-autocomplete",
        visible: u(g),
        interactive: "",
        "max-width": "100%",
        "hide-on-click": !1,
        placement: u(he),
        theme: "light-border",
        trigger: "manual",
        "data-cy": e.inputCy,
        onHidden: t[1] || (t[1] = (i) => g.value = !1),
        onClick: t[2] || (t[2] = (i) => P())
      }, {
        trigger: f(() => [
          I("div", Ze, [
            u(_) !== u(T).SEARCH_ONLY ? (s(!0), h(R, { key: 0 }, H(u(G), (i, p) => (s(), h("div", {
              key: p,
              class: "kapix-generated-chip"
            }, [
              ne((s(), S(l, {
                close: u(j),
                style: K(te(i)),
                item: i
              }, {
                close: f(() => [
                  u(j) ? (s(), S(a, {
                    key: 0,
                    "data-cy": e.inputCy && `${e.inputCy}-chip-icon-${p}`,
                    class: "kapix-chip-icon",
                    onClick: (oe) => Ie(p)
                  }, null, 8, ["data-cy", "onClick"])) : z("", !0)
                ]),
                _: 2
              }, 1032, ["close", "style", "item"])), [
                [c, i.description && { content: i.description }]
              ])
            ]))), 128)) : z("", !0),
            u(_) !== u(T).CHIPS_ONLY ? (s(), S(Ue, {
              key: u(pe),
              modelValue: u(k),
              "onUpdate:modelValue": t[0] || (t[0] = (i) => Te(k) ? k.value = i : null),
              "data-cy": e.inputCy && `${e.inputCy}-search`,
              class: "kapix-autocomplete-search-input",
              options: e.options,
              disabled: e.disabled,
              onKeypress: $e(Ce, ["enter"])
            }, {
              "prepend-icon": f(() => [
                L(e.$slots, "prepend-icon")
              ]),
              "prepend-inner-icon": f(() => [
                L(e.$slots, "prepend-inner-icon")
              ]),
              "append-icon": f(() => [
                L(e.$slots, "append-icon")
              ]),
              "append-outer-icon": f(() => [
                L(e.$slots, "append-outer-icon")
              ]),
              _: 3
            }, 8, ["modelValue", "data-cy", "options", "disabled"])) : z("", !0)
          ])
        ]),
        default: f(() => [
          u(g) && u(_) !== u(T).CHIPS_ONLY ? (s(), h("div", {
            key: 0,
            ref_key: "menu",
            ref: W,
            class: "kapix-autocomplete-input-menu-container",
            style: K(u(ve))
          }, [
            I("div", et, [
              (s(!0), h(R, null, H(u(xe), (i, p) => (s(), h("div", { key: p }, [
                I("div", {
                  class: "kapix-autocomplete-input-item",
                  "data-cy": e.inputCy && `${e.inputCy}-item-${p}`,
                  onClick: (oe) => X(i)
                }, [
                  I("div", {
                    class: "kapix-autocomplete-input-item-content",
                    innerHTML: i.label
                  }, null, 8, ot)
                ], 8, tt)
              ]))), 128))
            ], 512)
          ], 4)) : z("", !0)
        ]),
        _: 3
      }, 8, ["visible", "placement", "data-cy"])) : (s(), h("div", at, [
        (s(!0), h(R, null, H(u(G), (i, p) => (s(), h("div", {
          key: p,
          class: "kapix-generated-chip"
        }, [
          ne(qe(l, {
            style: K(te(i)),
            item: i
          }, null, 8, ["style", "item"]), [
            [c, i.description && { content: i.description }]
          ])
        ]))), 128))
      ]));
    };
  }
};
export {
  mt as default
};
