import { c as f, e as b } from "./es6--XPcqeHG-D1VmO9CH.js";
const {
  WritableStream: y,
  TransformStream: _,
  DOMException: P,
  Blob: m
} = f, {
  GONE: v
} = b, M = /constructor/i.test(window.HTMLElement);
class x {
  constructor(e = "unkown") {
    this.name = e, this.kind = "file";
  }
  async getFile() {
    throw new P(...v);
  }
  async isSameEntry(e) {
    return this === e;
  }
  /**
   * @param {object} [options={}]
   */
  async createWritable(e = {}) {
    const t = await navigator.serviceWorker?.getRegistration(), a = document.createElement("a"), o = new _(), u = o.writable;
    if (a.download = this.name, M || !t) {
      let r = [];
      o.readable.pipeTo(new y({
        write(s) {
          r.push(new m([s]));
        },
        close() {
          const s = new m(r, {
            type: "application/octet-stream; charset=utf-8"
          });
          r = [], a.href = URL.createObjectURL(s), a.click(), setTimeout(() => URL.revokeObjectURL(a.href), 1e4);
        }
      }));
    } else {
      const {
        writable: r,
        readablePort: s
      } = new O(y), n = encodeURIComponent(this.name).replace(/['()]/g, escape).replace(/\*/g, "%2A"), g = {
        "content-disposition": "attachment; filename*=UTF-8''" + n,
        "content-type": "application/octet-stream; charset=utf-8",
        ...e.size ? {
          "content-length": e.size
        } : {}
      }, w = setTimeout(() => t.active.postMessage(0), 1e4);
      o.readable.pipeThrough(new _({
        transform(c, h) {
          if (c instanceof Uint8Array) return h.enqueue(c);
          const R = new Response(c).body.getReader(), d = (S) => R.read().then((p) => p.done ? 0 : d(h.enqueue(p.value)));
          return d();
        }
      })).pipeTo(r).finally(() => {
        clearInterval(w);
      }), t.active.postMessage({
        url: t.scope + n,
        headers: g,
        readablePort: s
      }, [s]);
      const i = document.createElement("iframe");
      i.hidden = !0, i.src = t.scope + n, document.body.appendChild(i);
    }
    return u.getWriter();
  }
}
const E = 0, T = 0, U = 1, j = 1, k = 2;
class L {
  /** @param {MessagePort} port */
  constructor(e) {
    e.onmessage = (t) => this._onMessage(t.data), this._port = e, this._resetReady();
  }
  start(e) {
    return this._controller = e, this._readyPromise;
  }
  write(e) {
    const t = {
      type: E,
      chunk: e
    };
    return this._port.postMessage(t, [e.buffer]), this._resetReady(), this._readyPromise;
  }
  close() {
    this._port.postMessage({
      type: k
    }), this._port.close();
  }
  abort(e) {
    this._port.postMessage({
      type: j,
      reason: e
    }), this._port.close();
  }
  _onMessage(e) {
    e.type === T && this._resolveReady(), e.type === U && this._onError(e.reason);
  }
  _onError(e) {
    this._controller.error(e), this._rejectReady(e), this._port.close();
  }
  _resetReady() {
    this._readyPromise = new Promise((e, t) => {
      this._readyResolve = e, this._readyReject = t;
    }), this._readyPending = !0;
  }
  _resolveReady() {
    this._readyResolve(), this._readyPending = !1;
  }
  _rejectReady(e) {
    this._readyPending || this._resetReady(), this._readyPromise.catch(() => {
    }), this._readyReject(e), this._readyPending = !1;
  }
}
class O {
  constructor(e) {
    const t = new MessageChannel();
    this.readablePort = t.port1, this.writable = new e(new L(t.port2));
  }
}
export {
  x as FileHandle
};
//# sourceMappingURL=downloader-DcCTL3Wz-DLn-B9X4.js.map
