"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Datasource_exports = {};
__export(Datasource_exports, {
  Datasource: () => Datasource
});
module.exports = __toCommonJS(Datasource_exports);
var import_general = require("@matter/general");
var import_protocol = require("@matter/protocol");
var import_SupervisionConfig = require("../../supervision/SupervisionConfig.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("Datasource");
const FEATURES_KEY = "__features__";
const viewTx = import_general.Transaction.open("offline-view", import_general.Lifetime.process, "ro");
function Datasource(options) {
  return new DatasourceImpl(options);
}
((Datasource2) => {
  Datasource2.UNKNOWN_VERSION = -1;
})(Datasource || (Datasource = {}));
class DatasourceImpl {
  // From Datasource.Options
  type;
  supervisor;
  location;
  entropy;
  store;
  defaults;
  owner;
  onChange;
  // Computed state
  primaryKey;
  version;
  manageVersion;
  events;
  sessions;
  featuresKey;
  featuresKeyPersisted;
  persistentFields;
  supervisionConfig;
  observedInteractions;
  #values;
  #changedEventIndex;
  #readOnlyView;
  constructor(options) {
    this.type = options.type;
    this.supervisor = options.supervisor;
    this.location = options.location;
    this.entropy = options.entropy;
    this.store = options.store;
    this.defaults = options.defaults;
    this.owner = options.owner;
    this.onChange = options.onChange;
    this.primaryKey = options.primaryKey === "id" ? "id" : "name";
    this.events = options.events ?? {};
    const values = new options.type();
    let storedValues = options.store?.initialValues;
    if (options.supervisor.featureMap.children.length) {
      this.featuresKey = [...options.supervisor.supportedFeatures].join(",");
      const storedFeaturesKey = storedValues?.[FEATURES_KEY];
      if (storedFeaturesKey !== void 0) {
        if (storedFeaturesKey !== this.featuresKey) {
          logger.warn(
            `Ignoring persisted values for ${options.location.path} because features changed from "${storedFeaturesKey}" to "${this.featuresKey}"`
          );
          storedValues = void 0;
        } else {
          this.featuresKeyPersisted = true;
        }
      }
    }
    const initialValues = {
      ...options.defaults,
      ...storedValues
    };
    if (FEATURES_KEY in initialValues) {
      delete initialValues[FEATURES_KEY];
    }
    for (const key in initialValues) {
      values[key] = initialValues[key];
    }
    this.#values = values;
    Object.freeze(options.location);
    this.version = options.entropy.randomUint32;
    this.manageVersion = true;
    this.persistentFields = options.supervisor.persistentKeys(options.primaryKey);
    this.#configureExternalChanges();
  }
  // -- Datasource interface --
  toString() {
    return this.location.path.toString();
  }
  reference(session) {
    let ref = this.sessions?.get(session);
    if (!ref) {
      ref = createReference(this, this, session);
    }
    return ref.managed;
  }
  close() {
    if (this.observedInteractions) {
      for (const observable of this.observedInteractions) {
        observable.off(this.interactionObserver);
      }
      this.observedInteractions = void 0;
    }
    const store = this.store;
    if (store?.consumer === this) {
      store.consumer = void 0;
    }
  }
  validate(session, values) {
    const validate = this.supervisor.validate;
    if (!validate) {
      return;
    }
    validate(values ?? this.#values, session, {
      path: this.location.path,
      config: this.supervisionConfig
    });
  }
  get view() {
    if (!this.#readOnlyView) {
      const session = {
        transaction: viewTx,
        supervisionMode: "global"
      };
      this.#readOnlyView = createReference(this, this, session).managed;
    }
    return this.#readOnlyView;
  }
  // -- Internal methods (used by RootReference) --
  get values() {
    return this.#values;
  }
  set values(newValues) {
    const oldValues = this.#values;
    this.#values = newValues;
    if (this.sessions) {
      for (const context of this.sessions.values()) {
        context.onChange(oldValues);
      }
    }
  }
  interactionObserver = (session) => {
    if ((0, import_protocol.hasRemoteActor)(session)) {
      if (session.interactionComplete) {
        session.interactionComplete.off(this.interactionObserver);
        this.observedInteractions?.delete(session.interactionComplete);
      }
      session.interactionStarted = false;
    }
    const location = this.location;
    function handleObserverError(error) {
      logger.error(`Error in ${location.path} observer:`, error);
    }
    if (this.events?.interactionEnd?.isObserved) {
      try {
        const result = this.events?.interactionEnd?.emit(session);
        if (import_general.MaybePromise.is(result)) {
          return import_general.MaybePromise.then(result, void 0, handleObserverError);
        }
      } catch (e) {
        handleObserverError(e);
      }
    }
  };
  changedEventFor(key) {
    if (this.#changedEventIndex === void 0) {
      this.#changedEventIndex = /* @__PURE__ */ new Map();
    } else if (this.#changedEventIndex.has(key)) {
      return this.#changedEventIndex.get(key);
    }
    const id = Number.parseInt(key);
    let event;
    if (!Number.isFinite(id)) {
      event = this.events[`${key}$Changed`];
    } else {
      const field = this.supervisor.schema.member(id);
      if (field !== void 0) {
        event = this.events[`${field.propertyName}$Changed`];
      }
    }
    this.#changedEventIndex.set(key, event);
    return event;
  }
  // -- External change handling --
  #configureExternalChanges() {
    const { store } = this;
    if (!store || !("externalSet" in store)) {
      return;
    }
    const externalStore = store;
    this.version = externalStore.version;
    this.manageVersion = false;
    externalStore.consumer = this;
  }
  // -- Datasource.ExternallyMutableStore.Consumer --
  async integrateExternalChange(potentialChanges) {
    const { values } = this;
    const externalStore = this.store;
    let changes;
    let oldValues;
    for (const [key, newValue] of potentialChanges) {
      const name = String(key);
      if ((0, import_general.isDeepEqual)(values[name], newValue)) {
        continue;
      }
      if (changes === void 0) {
        changes = /* @__PURE__ */ new Map([[name, newValue]]);
        oldValues = /* @__PURE__ */ new Map([[name, values[name]]]);
      } else {
        changes.set(name, newValue);
        oldValues.set(name, values[name]);
      }
    }
    this.version = externalStore.version;
    if (!changes) {
      return;
    }
    this.values = {
      ...this.values,
      ...Object.fromEntries(changes)
    };
    const changedProps = Array.from(changes.keys());
    const onChangePromise = this.onChange?.(changedProps);
    const iterator = changedProps[Symbol.iterator]();
    const self = this;
    if (onChangePromise) {
      return onChangePromise.then(emitChanged);
    }
    return emitChanged();
    function emitChanged() {
      while (true) {
        const n = iterator.next();
        if (n.done) {
          return;
        }
        const name = n.value;
        const event = self.changedEventFor(name);
        if (!event?.isObserved) {
          continue;
        }
        const result = event.emit(changes.get(name), oldValues.get(name));
        if (import_general.MaybePromise.is(result)) {
          return Promise.resolve(result).then(emitChanged);
        }
      }
    }
  }
  readValues(keys) {
    const result = {};
    for (const key of keys) {
      if (key in this.#values) {
        result[key] = this.#values[key];
      }
    }
    return result;
  }
  releaseValues() {
    const { values } = this;
    this.values = {};
    return values;
  }
}
class RootReference {
  primaryKey;
  subrefs;
  owner;
  supervisionConfig;
  #values;
  #precommitValues;
  #changes;
  #expired = false;
  #internals;
  #session;
  #resource;
  #fields;
  #context;
  constructor(resource, internals, session) {
    this.#resource = resource;
    this.#internals = internals;
    this.#session = session;
    this.#values = internals.values;
    this.#fields = internals.supervisor.memberNames;
    this.primaryKey = internals.primaryKey;
    const transaction = session.transaction;
    void transaction.onShared(() => {
      if (this.#values !== this.#internals.values) {
        try {
          this.rollback();
        } catch (e) {
          logger.error(
            `Error resetting reference to ${this.#internals.location.path} after reset of transaction ${transaction.via}:`,
            e
          );
        }
      }
    });
    if (!internals.supervisionConfig) {
      internals.supervisionConfig = new import_SupervisionConfig.GlobalConfig();
    }
    if (session.supervisionMode === "global") {
      this.supervisionConfig = internals.supervisionConfig;
    } else {
      this.supervisionConfig = new import_SupervisionConfig.LocalConfig(internals.supervisionConfig);
    }
  }
  /**
   * Complete initialization after the managed value is created.  Must be called immediately after construction.
   */
  initialize() {
    const internals = this.#internals;
    const session = this.#session;
    const transaction = session.transaction;
    this.#context = {
      managed: internals.supervisor.manage(this, session),
      onChange: (oldValues) => {
        if (this.#values === oldValues) {
          this.#values = this.#internals.values;
          this.#refreshSubrefs();
        }
      }
    };
    if (transaction.isolation !== "snapshot") {
      if (!internals.sessions) {
        internals.sessions = /* @__PURE__ */ new Map();
      }
      internals.sessions.set(session, this.#context);
    }
    void transaction.onClose(() => {
      try {
        this.#internals.sessions?.delete(this.#session);
        this.#expired = true;
        this.#refreshSubrefs();
      } catch (e) {
        logger.error(
          `Error detaching reference to ${this.#internals.location.path} from closed transaction ${transaction.via}:`,
          e
        );
      }
    });
    return this.#context;
  }
  toString() {
    return `ref<${this.#resource}>`;
  }
  // -- ValReference implementation --
  get original() {
    return this.#internals.values;
  }
  get value() {
    if (this.#expired) {
      throw new import_protocol.ExpiredReferenceError(this.location);
    }
    return this.#values;
  }
  set value(_value) {
    throw new import_general.InternalError(`Cannot set root reference for ${this.#internals.supervisor.schema.name}`);
  }
  get expired() {
    return this.#expired;
  }
  get location() {
    return this.#internals.location;
  }
  set location(_loc) {
    throw new import_general.ImplementationError("Root reference location is immutable");
  }
  get rootOwner() {
    return this.#internals.owner;
  }
  change(mutator) {
    if (this.#expired) {
      throw new import_protocol.ExpiredReferenceError(this.location);
    }
    this.#startWrite();
    this.#session.transaction.beginSync();
    if (this.#values === this.#internals.values) {
      const old = this.#values;
      this.#values = new this.#internals.type();
      const properties = this.#values[import_protocol.Val.properties] ? this.#values[import_protocol.Val.properties](this.rootOwner, this.#session) : void 0;
      const keys = new Set(this.#fields);
      for (const key of Object.keys(old)) {
        keys.add(key);
      }
      for (const key of keys) {
        if (properties && key in properties) {
          continue;
        }
        this.#values[key] = old[key];
      }
      this.#refreshSubrefs();
    }
    mutator();
    this.#refreshSubrefs();
  }
  refresh() {
    throw new import_general.InternalError(`Cannot refresh root reference for ${this.#internals.supervisor.schema.name}`);
  }
  // -- Transaction.Participant implementation --
  /**
   * For pre-commit we trigger "fieldName$Changing" events for any fields that have changed since the previous
   * pre-commit cycle.
   *
   * Tracking data here is relatively expensive so we limit to events with registered observers.
   */
  preCommit() {
    const { events } = this.#internals;
    if (!events) {
      return false;
    }
    let mayHaveMutated = false;
    const keyIterator = Object.keys(this.#values)[Symbol.iterator]();
    const nextKey = () => {
      while (true) {
        const n = keyIterator.next();
        if (n.done) {
          return mayHaveMutated;
        }
        const name = n.value;
        const event = events?.[`${name}$Changing`];
        if (!event?.isObserved) {
          continue;
        }
        const change = this.#computePreCommitChange(name);
        if (change) {
          mayHaveMutated = true;
          const result = event.emit(change.newValue, change.oldValue, this.#session);
          if (import_general.MaybePromise.is(result)) {
            return result.then(nextKey);
          }
        }
      }
    };
    return nextKey();
  }
  /**
   * For commit phase one we pass values to the persistence layer if present.
   */
  commit1() {
    this.#computePostCommitChanges();
    const persistent = this.#changes?.persistent;
    if (!persistent) {
      return;
    }
    if (this.#internals.featuresKey !== void 0 && !this.#internals.featuresKeyPersisted) {
      persistent[FEATURES_KEY] = this.#internals.featuresKey;
      this.#internals.featuresKeyPersisted = true;
    }
    return this.#internals.store?.set(this.#session.transaction, persistent);
  }
  /**
   * For commit phase two we make the working values canonical and notify listeners.
   */
  commit2() {
    if (!this.#changes) {
      return;
    }
    this.#internals.values = this.#values;
  }
  /**
   * Post-commit logic.  Emit "changed" events.  Observers may be synchronous or asynchronous.
   */
  postCommit() {
    if (!this.#changes) {
      return;
    }
    const iterator = this.#changes.notifications[Symbol.iterator]();
    function emitChanged() {
      while (true) {
        const n = iterator.next();
        if (n.done) {
          return;
        }
        const { event, params } = n.value;
        const result = event.emit(...params);
        if (import_general.MaybePromise.is(result)) {
          return Promise.resolve(result).then(emitChanged);
        }
      }
    }
    const onChangePromise = this.#internals.onChange?.([...this.#changes.changeList]);
    if (onChangePromise) {
      return onChangePromise.then(emitChanged);
    }
    return emitChanged();
  }
  /**
   * On rollback, we just replace values and version with the canonical versions.
   */
  rollback() {
    this.#values = this.#internals.values;
    this.#refreshSubrefs();
  }
  // -- Private helpers --
  #startWrite() {
    const transaction = this.#session.transaction;
    transaction.addResourcesSync(this.#resource);
    transaction.addParticipants(this);
    transaction.beginSync();
    if ((0, import_protocol.hasRemoteActor)(this.#session) && !this.#session.interactionStarted && this.#session.interactionComplete && !this.#session.interactionComplete.isObservedBy(this.#internals.interactionObserver)) {
      this.#session.interactionStarted = true;
      if (this.#internals.events?.interactionBegin?.isObserved) {
        this.#internals.events?.interactionBegin?.emit(this.#session);
      }
      if (!this.#internals.observedInteractions) {
        this.#internals.observedInteractions = /* @__PURE__ */ new Set();
      }
      this.#internals.observedInteractions.add(this.#session.interactionComplete);
      this.#session.interactionComplete.on(this.#internals.interactionObserver);
    }
  }
  #refreshSubrefs() {
    const subrefs = this.subrefs;
    if (subrefs) {
      for (const key in subrefs) {
        subrefs[key].refresh();
      }
    }
  }
  #incrementVersion() {
    if (!this.#internals.manageVersion) {
      return;
    }
    this.#internals.version++;
    if (this.#internals.version > 4294967295) {
      this.#internals.version = 0;
    }
  }
  #computePreCommitChange(name) {
    let oldValue;
    if (this.#precommitValues && name in this.#precommitValues) {
      oldValue = this.#precommitValues[name];
    } else {
      oldValue = this.#internals.values[name];
    }
    const newValue = this.#values[name];
    if ((0, import_general.isDeepEqual)(oldValue, newValue)) {
      return;
    }
    if (!this.#precommitValues) {
      this.#precommitValues = {};
    }
    this.#precommitValues[name] = (0, import_general.deepCopy)(newValue);
    return { newValue: this.#context.managed[name], oldValue };
  }
  #computePostCommitChanges() {
    this.#changes = void 0;
    if (this.#internals.values === this.#values) {
      return;
    }
    for (const name in this.#values) {
      const newval = this.#values[name];
      const oldval = this.#internals.values[name];
      if (oldval !== newval && !(0, import_general.isDeepEqual)(newval, oldval)) {
        if (!this.#changes) {
          this.#changes = { notifications: [], changeList: /* @__PURE__ */ new Set() };
        }
        this.#changes.changeList.add(name);
        if (this.#internals.persistentFields.has(name)) {
          if (this.#changes.persistent === void 0) {
            this.#changes.persistent = {};
          }
          this.#changes.persistent[name] = this.#values[name];
        }
        const event = this.#internals.changedEventFor(name);
        if (event?.isObserved) {
          this.#changes.notifications.push({
            event,
            params: [this.#values[name], this.#internals.values[name], this.#session]
          });
        }
      }
    }
    if (this.#changes) {
      this.#incrementVersion();
      if (this.#internals.events.stateChanged?.isObserved) {
        this.#changes.notifications.push({
          event: this.#internals.events.stateChanged,
          params: [this.#session]
        });
      }
    }
  }
}
function createReference(resource, internals, session) {
  const ref = new RootReference(resource, internals, session);
  return ref.initialize();
}
//# sourceMappingURL=Datasource.js.map
