import { defineComponent as G, reactive as J, computed as h, watch as V, toRef as Q, onMounted as X, createElementBlock as d, openBlock as i, withModifiers as I, normalizeClass as n, unref as s, createElementVNode as c, createVNode as f, Fragment as g, renderList as K, createBlock as Y, createCommentVNode as Z, toDisplayString as v, withDirectives as L, createTextVNode as O } from "vue";
import { useEventListener as ee } from "../../composition/useEventListener.js";
import { notEmpty as A, between as te, tapIf as le } from "@milaboratories/helpers";
import { getRawPlatformaInstance as y } from "@platforma-sdk/model";
import ae from "../PlDropdown/PlDropdown.vue.js";
import b from "../PlIcon16/PlIcon16.vue.js";
import se from "./Shortcuts.vue.js";
import oe from "../PlSearchField/PlSearchField.vue.js";
import r from "./pl-file-dialog.module.scss.js";
import { defaultData as M, useVisibleItems as re, vTextOverflown as H } from "./remote.js";
import { normalizeExtensions as ne, getFilePathBreadcrumbs as ie } from "./utils.js";
const ce = ["title", "onClick"], de = {
  key: 0,
  class: "ls-loader"
}, me = ["onClick"], ue = ["title"], pe = ["onClick"], he = ["title"], _e = /* @__PURE__ */ G({
  __name: "Remote",
  props: {
    modelValue: { type: Boolean },
    extensions: { default: void 0 },
    multi: { type: Boolean },
    title: { default: void 0 },
    autoSelectStorage: { type: Boolean, default: !0 },
    submit: {}
  },
  emits: ["update:modelValue", "import:files"],
  setup(N, { expose: R }) {
    const E = (e) => e.ctrlKey || e.metaKey, u = N, t = J(M()), k = () => {
      t.search = "", t.error = "", t.lastSelected = void 0;
    }, _ = h(() => ne(u.extensions)), S = re(t), T = h(() => {
      var e;
      return {
        modelValue: u.modelValue,
        dirPath: t.dirPath,
        storageHandle: (e = t.storageEntry) == null ? void 0 : e.handle
      };
    }), $ = (e, l) => {
      y() && t.currentLoadingPath !== l && (t.currentLoadingPath = l, y().lsDriver.listFiles(e, l).then((a) => {
        l === t.dirPath && (t.items = A(a).entries.map((o) => ({
          path: o.fullPath,
          name: o.name,
          isDir: o.type === "dir",
          canBeSelected: o.type === "file" && (!_.value || _.value.some((m) => o.fullPath.endsWith(m))),
          handle: o.type === "file" ? o.handle : void 0,
          selected: !1
        })).sort((o, m) => o.isDir && !m.isDir ? -1 : !o.isDir && m.isDir ? 1 : o.name.localeCompare(m.name)).map((o, m) => ({ id: m, ...o })), t.lastSelected = void 0);
      }).catch((a) => t.error = String(a)).finally(() => {
        t.currentLoadingPath = void 0;
      }));
    }, z = () => {
      k();
      const { storageHandle: e, dirPath: l, modelValue: a } = T.value;
      e && a && $(e, l);
    }, B = h(() => ie(t.dirPath)), P = h(() => t.items.filter((e) => e.canBeSelected && e.selected && !e.isDir)), U = h(() => {
      var e;
      return P.value.length > 0 && ((e = t.storageEntry) == null ? void 0 : e.handle);
    }), W = () => {
      var e;
      return {
        storageHandle: A((e = t.storageEntry) == null ? void 0 : e.handle),
        files: P.value.map((l) => l.handle)
      };
    }, x = (e) => {
      t.dirPath = e;
    }, j = (e, l) => {
      const { shiftKey: a } = e, o = E(e), { lastSelected: m } = t;
      e.preventDefault();
      const D = S.value;
      if (l.canBeSelected) {
        if (u.multi || D.forEach((p) => p.selected = !1), l.selected = !l.selected, !u.multi)
          return;
        !o && !a && D.forEach((p) => {
          p.id !== l.id && (p.selected = !1);
        }), a && m !== void 0 && D.forEach((p) => {
          te(p.id, m, l.id) && (p.selected = !0);
        }), l.selected && (t.lastSelected = l.id);
      }
    }, C = (e) => {
      e && !u.multi || S.value.filter((l) => l.canBeSelected).forEach((l) => {
        l.selected = e;
      });
    }, q = () => C(!0), w = () => C(!1), F = () => {
      if (k(), w(), !y()) {
        console.warn("platforma API is not found");
        return;
      }
      y().lsDriver.getStorageList().then((e) => {
        e = e.filter((l) => l.name !== "local" && !l.name.startsWith("local_disk_")), t.storageOptions = e.map((l) => ({
          text: l.name,
          value: l
        })), u.autoSelectStorage && le(e[0], (l) => {
          t.storageEntry = l;
        });
      }).catch((e) => t.error = String(e));
    };
    return V(
      Q(t, "storageEntry"),
      (e) => {
        k(), t.dirPath = (e == null ? void 0 : e.initialFullPath) ?? "";
      },
      { immediate: !0 }
    ), V([() => t.dirPath, () => t.storageEntry], () => {
      z();
    }), V(
      () => u.modelValue,
      (e) => {
        e ? F() : Object.assign(t, M());
      },
      { immediate: !0 }
    ), ee(document, "keydown", (e) => {
      if (!u.modelValue || e.target !== document.body)
        return;
      const l = E(e);
      l && e.code === "KeyA" && (e.preventDefault(), q()), l && e.shiftKey && e.code === "Period" && (e.preventDefault(), t.showHiddenItems = !t.showHiddenItems), e.code === "Enter" && u.submit();
    }), R({
      isReady: U,
      getFilesToImport: W
    }), X(F), (e, l) => (i(), d("div", {
      class: n(s(r).remote),
      onClick: I(w, ["stop"])
    }, [
      c("div", {
        class: n(s(r).search)
      }, [
        c("div", null, [
          f(s(ae), {
            modelValue: t.storageEntry,
            "onUpdate:modelValue": l[0] || (l[0] = (a) => t.storageEntry = a),
            label: "Select storage",
            options: t.storageOptions
          }, null, 8, ["modelValue", "options"])
        ]),
        c("div", null, [
          f(s(oe), {
            modelValue: t.search,
            "onUpdate:modelValue": l[1] || (l[1] = (a) => t.search = a),
            label: "Search in folder",
            clearable: ""
          }, null, 8, ["modelValue"])
        ])
      ], 2),
      c("div", {
        class: n(s(r)["ls-container"])
      }, [
        c("div", {
          class: n(s(r)["ls-head"])
        }, [
          c("div", {
            class: n(s(r).breadcrumbs)
          }, [
            (i(!0), d(g, null, K(B.value, (a, o) => (i(), d(g, { key: o }, [
              c("div", {
                title: a.path,
                onClick: (m) => x(a.path)
              }, v(a.name), 9, ce),
              a.index !== B.value.length - 1 ? (i(), Y(s(b), {
                key: 0,
                name: "chevron-right"
              })) : Z("", !0)
            ], 64))), 128))
          ], 2),
          c("div", {
            class: n(s(r).selected)
          }, [
            c("span", null, "Selected: " + v(P.value.length), 1),
            f(se)
          ], 2)
        ], 2),
        t.currentLoadingPath !== void 0 ? (i(), d("div", de, l[2] || (l[2] = [
          c("i", { class: "mask-24 mask-loading loader-icon" }, null, -1)
        ]))) : t.storageEntry ? t.error ? (i(), d("div", {
          key: 2,
          class: n(s(r)["ls-error"])
        }, [
          c("div", {
            class: n(s(r).cat)
          }, null, 2),
          c("div", {
            class: n(s(r).message)
          }, v(t.error), 3)
        ], 2)) : (i(), d("div", {
          key: 3,
          class: n(s(r)["ls-body"])
        }, [
          (i(!0), d(g, null, K(s(S), (a) => (i(), d(g, {
            key: a.id
          }, [
            a.isDir ? (i(), d("div", {
              key: 0,
              class: n(s(r).isDir),
              onClick: (o) => x(a.path)
            }, [
              f(s(b), { name: "chevron-right" }),
              L((i(), d("span", {
                title: a.name
              }, [
                O(v(a.name), 1)
              ], 8, ue)), [
                [s(H)]
              ])
            ], 10, me)) : (i(), d("div", {
              key: 1,
              class: n({ [s(r).canBeSelected]: a.canBeSelected, [s(r).selected]: a.selected }),
              onClick: I((o) => j(o, a), ["stop"])
            }, [
              f(s(b), {
                name: "box",
                class: n(s(r).isFile)
              }, null, 8, ["class"]),
              L((i(), d("span", {
                title: a.name
              }, [
                O(v(a.name), 1)
              ], 8, he)), [
                [s(H)]
              ])
            ], 10, pe))
          ], 64))), 128))
        ], 2)) : (i(), d("div", {
          key: 1,
          class: n(s(r)["ls-empty"])
        }, [
          c("div", {
            class: n(s(r).cat)
          }, null, 2),
          c("div", {
            class: n(s(r).message)
          }, "Select storage to preview", 2)
        ], 2))
      ], 2)
    ], 2));
  }
});
export {
  _e as default
};
//# sourceMappingURL=Remote.vue.js.map
