import { jsxs as T, jsx as s } from "react/jsx-runtime";
import { findNodeById as x, getAncestors as C, useEdificeClient as S, Modal as m, TreeView as w, Button as b } from "@edifice.io/react";
import { createPortal as F } from "react-dom";
import { useTranslation as g } from "react-i18next";
import { useQueryClient as A } from "@tanstack/react-query";
import { useState as B } from "react";
import { m as D, s as E, t as k, z as U, w as j } from "./index2.js";
function O({
  onSuccess: a
}) {
  const [d, n] = B(), u = A(), o = D(), h = E(), p = k(), i = U(), {
    fetchTreeData: v
  } = j();
  async function y() {
    try {
      if (!d) throw new Error("explorer.move.selection.empty");
      await o.mutate(d), await (a == null ? void 0 : a());
    } catch (t) {
      console.error(t);
    }
  }
  const I = (t) => {
    var f, c;
    const r = C(i, t);
    for (const e of h)
      if (t === e.id || t === e.parentId || (f = e.childrenIds) != null && f.includes(t) || r.includes(e.id))
        return !1;
    for (const e of p)
      if (t === ((e == null ? void 0 : e.folderIds) && e.folderIds[0]) || ((c = e == null ? void 0 : e.folderIds) == null ? void 0 : c.length) === 0 && t === "default")
        return !1;
    return !0;
  };
  return {
    treeData: i,
    disableSubmit: !d,
    handleTreeItemSelect: (t) => {
      I(t) ? n(t) : n(void 0);
    },
    handleOnTreeItemUnfold: (t) => {
      var c, e;
      const r = x(i, t), f = (c = r == null ? void 0 : r.children) == null ? void 0 : c.some((l) => {
        var M;
        return Array.isArray(l == null ? void 0 : l.children) && ((M = l.children) == null ? void 0 : M.length) > 0;
      });
      (e = r == null ? void 0 : r.children) == null || e.forEach((l) => {
        f || v(l.id, u);
      });
    },
    onMove: () => y()
  };
}
function W({
  isOpen: a,
  onSuccess: d,
  onCancel: n
}) {
  const {
    appCode: u
  } = S(), {
    t: o
  } = g(), {
    treeData: h,
    handleTreeItemSelect: p,
    handleOnTreeItemUnfold: i,
    onMove: v,
    disableSubmit: y
  } = O({
    onSuccess: d
  }), I = {
    ...h,
    name: o("explorer.filters.mine", {
      ns: u
    })
  };
  return /* @__PURE__ */ F(/* @__PURE__ */ T(m, { isOpen: a, onModalClose: n, id: "moveModal", children: [
    /* @__PURE__ */ s(m.Header, { onModalClose: n, children: o("explorer.move.title") }),
    /* @__PURE__ */ s(m.Subtitle, { children: o("explorer.move.subtitle") }),
    /* @__PURE__ */ s(m.Body, { children: /* @__PURE__ */ s(w, { data: I, onTreeItemClick: p, onTreeItemUnfold: i }) }),
    /* @__PURE__ */ T(m.Footer, { children: [
      /* @__PURE__ */ s(b, { color: "tertiary", onClick: n, type: "button", variant: "ghost", children: o("explorer.cancel") }),
      /* @__PURE__ */ s(b, { color: "primary", onClick: v, type: "button", variant: "filled", disabled: y, children: o("explorer.move") })
    ] })
  ] }), document.getElementById("portal"));
}
export {
  W as default
};
