import { promiseTrue, promiseFalse, promiseVoid } from "../../../shared/src/resolved-promises.js";
import { deepFreezeAllowUndefined, deepFreeze } from "../frozen-json.js";
import { transactionIsClosedRejection, maybeTransactionIsClosedRejection } from "./throw-if-closed.js";
const deleteSentinel = Symbol();
class WriteImplBase {
  _pending = /* @__PURE__ */ new Map();
  #read;
  constructor(read) {
    this.#read = read;
  }
  has(key) {
    if (this.#read.closed) {
      return transactionIsClosedRejection();
    }
    switch (this._pending.get(key)) {
      case void 0:
        return this.#read.has(key);
      case deleteSentinel:
        return promiseFalse;
      default:
        return promiseTrue;
    }
  }
  async get(key) {
    if (this.#read.closed) {
      return transactionIsClosedRejection();
    }
    const v = this._pending.get(key);
    switch (v) {
      case deleteSentinel:
        return void 0;
      case void 0: {
        const v2 = await this.#read.get(key);
        return deepFreezeAllowUndefined(v2);
      }
      default:
        return v;
    }
  }
  put(key, value) {
    return maybeTransactionIsClosedRejection(this.#read) ?? (this._pending.set(key, deepFreeze(value)), promiseVoid);
  }
  del(key) {
    return maybeTransactionIsClosedRejection(this.#read) ?? (this._pending.set(key, deleteSentinel), promiseVoid);
  }
  release() {
    this.#read.release();
  }
  get closed() {
    return this.#read.closed;
  }
}
export {
  WriteImplBase,
  deleteSentinel
};
//# sourceMappingURL=write-impl-base.js.map
