"use client";
import { useSyncExternalStore as h } from "react";
var n = class {
  constructor(t) {
    this.getSnapshot = () => this.store, this.subscribe = (s) => (this.subscribers.push(s), () => {
      const r = this.subscribers.indexOf(s);
      this.subscribers.splice(r, 1);
    }), this.notify = () => {
      this.subscribers.forEach((s) => s());
    }, this.end = () => {
      this.store = [
        this.store[0],
        {
          asking: !1,
          cancel: () => {
          },
          ok: () => {
          }
        }
      ], this.notify();
    }, this.ask = (s) => this.start(!1, s), this.safeAsk = (s) => this.start(!0, s), this.store = [
      {
        key: 0,
        payload: t
      },
      {
        asking: !1,
        cancel: () => {
        },
        ok: () => {
        }
      }
    ], this.subscribers = [];
  }
  start(t, s) {
    const { promise: r, resolve: i, reject: o } = Promise.withResolvers();
    return this.store = [
      {
        key: this.store[0].key + 1,
        payload: s
      },
      {
        asking: !!i && !!o,
        cancel: (e) => {
          t ? i({ ok: !1, reason: e }) : o(e), this.end();
        },
        ok: (e) => {
          i(t ? { ok: !0, data: e } : e), this.end();
        }
      }
    ], this.notify(), r;
  }
}, a = (t) => {
  const s = new n(t);
  return [s, () => h(s.subscribe, s.getSnapshot, s.getSnapshot)];
};
export {
  n as AskStore,
  a as createAsk
};
