/**
 * ProvenancePayload - Transferable Provenance as a Monoid
 *
 * Implements the provenance payload from Paper III (Computational Holography):
 * P = (mu_0, ..., mu_{n-1}) - an ordered sequence of tick patches.
 *
 * @module domain/services/provenance/ProvenancePayload
 */
import { reducePatches, createEmptyState, cloneState } from "../JoinReducer.js";
import WarpError from "../../errors/WarpError.js";
import BoundaryTransitionProvenance from "./BoundaryTransitionProvenance.js";
class ProvenancePayload {
    #provenance;
    constructor(patches = []) {
        if (!Array.isArray(patches)) {
            throw new WarpError('ProvenancePayload requires an array of patches', 'E_PROVENANCE_PAYLOAD_INVALID');
        }
        this.#provenance = new BoundaryTransitionProvenance(patches);
        Object.freeze(this);
    }
    static identity() {
        return new ProvenancePayload([]);
    }
    get length() {
        return this.#provenance.length;
    }
    concat(other) {
        if (!(other instanceof ProvenancePayload)) {
            throw new WarpError('concat requires a ProvenancePayload', 'E_PROVENANCE_PAYLOAD_CONCAT');
        }
        if (this.#provenance.length === 0) {
            return other;
        }
        if (other.#provenance.length === 0) {
            return this;
        }
        return new ProvenancePayload([...this.#provenance, ...other.#provenance]);
    }
    replay(initialState) {
        if (this.#provenance.length === 0) {
            return initialState ? cloneState(initialState) : createEmptyState();
        }
        return reducePatches(this.#provenance.entries(), initialState);
    }
    [Symbol.iterator]() {
        return this.#provenance[Symbol.iterator]();
    }
    at(index) {
        return this.#provenance.at(index);
    }
    slice(start = 0, end = this.#provenance.length) {
        return new ProvenancePayload(this.#provenance.entries().slice(start, end));
    }
    entries() {
        return this.#provenance.entries();
    }
    get provenance() {
        return this.#provenance;
    }
    static fromEntries(entries) {
        return new ProvenancePayload(entries);
    }
}
export default ProvenancePayload;
export { ProvenancePayload };
