import { __awaiter as s } from "../../../../../tslib@2.8.1/node_modules/tslib/tslib.es6.js";
import { toFileWithPath as u } from "./file.js";
const y = [
  // Thumbnail cache files for macOS and Windows
  ".DS_Store",
  // macOs
  "Thumbs.db"
  // Windows
];
function S(n) {
  return s(this, void 0, void 0, function* () {
    return a(n) && F(n.dataTransfer) ? v(n.dataTransfer, n.type) : g(n) ? E(n) : Array.isArray(n) && n.every((r) => "getFile" in r && typeof r.getFile == "function") ? w(n) : [];
  });
}
function F(n) {
  return a(n);
}
function g(n) {
  return a(n) && a(n.target);
}
function a(n) {
  return typeof n == "object" && n !== null;
}
function E(n) {
  return c(n.target.files).map((r) => u(r));
}
function w(n) {
  return s(this, void 0, void 0, function* () {
    return (yield Promise.all(n.map((e) => e.getFile()))).map((e) => u(e));
  });
}
function v(n, r) {
  return s(this, void 0, void 0, function* () {
    if (n.items) {
      const e = c(n.items).filter((i) => i.kind === "file");
      if (r !== "drop")
        return e;
      const t = yield Promise.all(e.map(P));
      return d(m(t));
    }
    return d(c(n.files).map((e) => u(e)));
  });
}
function d(n) {
  return n.filter((r) => y.indexOf(r.name) === -1);
}
function c(n) {
  if (n === null)
    return [];
  const r = [];
  for (let e = 0; e < n.length; e++) {
    const t = n[e];
    r.push(t);
  }
  return r;
}
function P(n) {
  if (typeof n.webkitGetAsEntry != "function")
    return h(n);
  const r = n.webkitGetAsEntry();
  return r && r.isDirectory ? p(r) : h(n, r);
}
function m(n) {
  return n.reduce((r, e) => [
    ...r,
    ...Array.isArray(e) ? m(e) : [e]
  ], []);
}
function h(n, r) {
  return s(this, void 0, void 0, function* () {
    var e;
    if (globalThis.isSecureContext && typeof n.getAsFileSystemHandle == "function") {
      const o = yield n.getAsFileSystemHandle();
      if (o === null)
        throw new Error(`${n} is not a File`);
      if (o !== void 0) {
        const f = yield o.getFile();
        return f.handle = o, u(f);
      }
    }
    const t = n.getAsFile();
    if (!t)
      throw new Error(`${n} is not a File`);
    return u(t, (e = r == null ? void 0 : r.fullPath) !== null && e !== void 0 ? e : void 0);
  });
}
function A(n) {
  return s(this, void 0, void 0, function* () {
    return n.isDirectory ? p(n) : T(n);
  });
}
function p(n) {
  const r = n.createReader();
  return new Promise((e, t) => {
    const i = [];
    function o() {
      r.readEntries((f) => s(this, void 0, void 0, function* () {
        if (f.length) {
          const l = Promise.all(f.map(A));
          i.push(l), o();
        } else
          try {
            const l = yield Promise.all(i);
            e(l);
          } catch (l) {
            t(l);
          }
      }), (f) => {
        t(f);
      });
    }
    o();
  });
}
function T(n) {
  return s(this, void 0, void 0, function* () {
    return new Promise((r, e) => {
      n.file((t) => {
        const i = u(t, n.fullPath);
        r(i);
      }, (t) => {
        e(t);
      });
    });
  });
}
export {
  S as fromEvent
};
//# sourceMappingURL=file-selector.js.map
