import { isBoolean as h } from "lodash-unified";
import { ref as v, computed as S } from "vue";
import { getStringName as l } from "../../item-group/helper.mjs";
import "../../../utils/index.mjs";
import { getTargetNode as g } from "./use-collapse-input-count.mjs";
import { isDef as w } from "../../../utils/is.mjs";
function C(r, s) {
  const a = new Set(r.map((e) => e.name)), m = /* @__PURE__ */ new Map();
  for (const e of s) {
    const n = String(l(g(e).props?.name));
    n && a.has(n) && m.set(n, e);
  }
  const i = /* @__PURE__ */ new Set(), o = [];
  for (const e of r) {
    const n = m.get(e.name);
    n && (o.push(n), i.add(n));
  }
  for (const e of s)
    i.has(e) || o.push(e);
  return o;
}
const D = (r) => {
  const s = v(!1);
  let a = !1;
  const m = S((o) => {
    const n = (r.value || []).map((d) => {
      const t = g(d).props;
      return {
        label: t.description || t.label || t.placeholder,
        name: l(t.name),
        isHidden: h(t.hidden) ? !!t.hidden : w(t.hidden)
      };
    });
    if (!o?.length || a)
      return a = !1, n;
    const p = new Map(o.map((d, f) => [d.name, f])), u = [...n].sort((d, f) => {
      const t = p.get(d.name), c = p.get(f.name);
      return t === void 0 && c === void 0 ? 0 : t === void 0 ? 1 : c === void 0 ? -1 : t - c;
    });
    return i(u), u;
  });
  function i(o) {
    const e = C(
      o || [],
      r.value || []
    );
    a = !0, s.value = !0, r.value = e;
  }
  return {
    items: m,
    handleClickConfirm: i,
    sorted: s
  };
};
export {
  D as useSorter
};
