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

//# sourceMappingURL=Remote.vue2.js.map