import { hasOwnProperty as hasOwnProp } from '@jsonjoy.com/util/lib/hasOwnProperty';
import { Point } from '../rga/Point';
import { Range } from '../rga/Range';
import { updateNode } from '../../../json-crdt/hash';
import { printTree } from 'tree-dump/lib/printTree';
import { SliceHeaderMask, SliceHeaderShift, SliceStacking, SliceTupleIndex, SliceStackingName, SliceTypeName, } from './constants';
import { CONST } from '../../../json-hash/hash';
import { Timestamp } from '../../../json-crdt-patch/clock';
import { prettyOneLine } from '../../../json-pretty';
import { validateType } from './util';
import { NodeBuilder, s } from '../../../json-crdt-patch';
import { JsonCrdtDiff } from '../../../json-crdt-diff/JsonCrdtDiff';
import { ObjApi } from '../../../json-crdt/model';
import { NestedType } from './NestedType';
/**
 * A persisted slice is a slice that is stored in a {@link Model}. It is used for
 * rich-text formatting and annotations.
 *
 * @todo Maybe rename to "saved", "stored", "mutable".
 */
export class PersistedSlice extends Range {
    model;
    txt;
    chunk;
    tuple;
    start;
    end;
    static deserialize(model, txt, chunk, tuple) {
        const header = +tuple.get(0).view();
        const id1 = tuple.get(1).view();
        const id2 = (tuple.get(2).view() || id1);
        if (typeof header !== 'number')
            throw new Error('INVALID_HEADER');
        if (!(id1 instanceof Timestamp))
            throw new Error('INVALID_ID');
        if (!(id2 instanceof Timestamp))
            throw new Error('INVALID_ID');
        const anchor1 = (header & SliceHeaderMask.X1Anchor) >>> SliceHeaderShift.X1Anchor;
        const anchor2 = (header & SliceHeaderMask.X2Anchor) >>> SliceHeaderShift.X2Anchor;
        const stacking = (header & SliceHeaderMask.Stacking) >>> SliceHeaderShift.Stacking;
        const rga = txt.str;
        const p1 = new Point(rga, id1, anchor1);
        const p2 = new Point(rga, id2, anchor2);
        const slice = new PersistedSlice(model, txt, chunk, tuple, stacking, p1, p2);
        validateType(slice.type());
        return slice;
    }
    /** @todo Use API node here. */
    rga;
    constructor(
    /** The `Model` where the slice is stored. */
    model, 
    /** The Peritext context. */
    txt, 
    /** The `arr` chunk of `arr` where the slice is stored. */
    chunk, 
    /** The `vec` node which stores the serialized contents of this slice. */
    tuple, stacking, start, end) {
        super(txt.str, start, end);
        this.model = model;
        this.txt = txt;
        this.chunk = chunk;
        this.tuple = tuple;
        this.start = start;
        this.end = end;
        this.rga = txt.str;
        this.id = chunk.id;
        this.stacking = stacking;
    }
    isSplit() {
        return this.stacking === SliceStacking.Marker;
    }
    tupleApi() {
        return this.model.api.wrap(this.tuple);
    }
    // protected setType(schema: NodeBuilder): void {
    //   this.tupleApi().set([
    //     [SliceTupleIndex.Type, schema.type(type as SliceTypeSteps)],
    //   ]);
    // }
    // ---------------------------------------------------------------- mutations
    set(start, end = start) {
        super.set(start, end);
        this.update({ range: this });
    }
    /**
     * Expand range left and right to contain all invisible space: (1) tombstones,
     * (2) anchors of non-deleted adjacent chunks.
     */
    expand() {
        super.expand();
        this.update({ range: this });
    }
    /** -------------------------------------------------- {@link MutableSlice} */
    id;
    stacking;
    update(params) {
        let updateHeader = false;
        const changes = [];
        const stacking = params.stacking;
        if (stacking !== undefined) {
            this.stacking = stacking;
            updateHeader = true;
        }
        const range = params.range;
        if (range) {
            updateHeader = true;
            changes.push([SliceTupleIndex.X1, s.con(range.start.id)], [SliceTupleIndex.X2, s.con(range.end.id)]);
            this.start = range.start;
            this.end = range.start === range.end ? range.end.clone() : range.end;
        }
        if (params.type !== undefined) {
            changes.push([SliceTupleIndex.Type, params.type instanceof NodeBuilder ? params.type : s.jsonCon(params.type)]);
        }
        if (hasOwnProp(params, 'data'))
            changes.push([SliceTupleIndex.Data, params.data]);
        if (updateHeader) {
            const header = (this.stacking << SliceHeaderShift.Stacking) +
                (this.start.anchor << SliceHeaderShift.X1Anchor) +
                (this.end.anchor << SliceHeaderShift.X2Anchor);
            changes.push([SliceTupleIndex.Header, s.con(header)]);
        }
        this.tupleApi().set(changes);
    }
    getStore() {
        const txt = this.txt;
        const sid = this.id.sid;
        let store = txt.savedSlices;
        if (sid === store.set.doc.clock.sid)
            return store;
        store = txt.localSlices;
        if (sid === store.set.doc.clock.sid)
            return store;
        store = txt.extraSlices;
        if (sid === store.set.doc.clock.sid)
            return store;
        return;
    }
    del() {
        const store = this.getStore();
        if (!store)
            return;
        store.del(this.id);
    }
    isDel() {
        return this.chunk.del;
    }
    // -------------------------------------------------- slice type manipulation
    typeNode() {
        return this.tuple.get(SliceTupleIndex.Type);
    }
    typeApi() {
        const node = this.typeNode();
        if (!node)
            return;
        return this.model.api.wrap(node);
    }
    nestedType() {
        return new NestedType(this);
    }
    type() {
        return this.typeNode()?.view();
    }
    typeSteps() {
        const type = this.type() ?? 0 /* SliceTypeCon.p */;
        return Array.isArray(type) ? type : [type];
    }
    // -------------------------------------------------- slice data manipulation
    data() {
        return this.tuple.get(SliceTupleIndex.Data)?.view();
    }
    dataNode() {
        const node = this.tuple.get(SliceTupleIndex.Data);
        return node && this.model.api.wrap(node);
    }
    dataAsObj() {
        const node = this.dataNode();
        if (!(node instanceof ObjApi)) {
            this.tupleApi().set([[SliceTupleIndex.Data, s.obj({})]]);
        }
        return this.dataNode();
    }
    /**
     * Overwrites the data of this slice with the given data.
     *
     * @param data Data to set for this slice. The data can be any JSON value, but
     *     it is recommended to use an object.
     */
    setData(data) {
        this.tupleApi().set([[SliceTupleIndex.Data, s.jsonCon(data)]]);
    }
    /**
     * Merges object data into the slice's data using JSON CRDT diffing.
     *
     * @param data Data to merge into the slice. If the data is an object, it will be
     *     merged with the existing data of the slice using JSON CRDT diffing.
     */
    mergeData(data) {
        const { model } = this;
        const diff = new JsonCrdtDiff(model);
        if (this.dataNode() instanceof ObjApi && !!data && typeof data === 'object' && !Array.isArray(data)) {
            const dataNode = this.dataAsObj();
            const patch = diff.diff(dataNode.node, data);
            model.applyPatch(patch);
        }
        else
            this.setData(data);
    }
    /** ------------------------------------------------------ {@link Stateful} */
    hash = 0;
    refresh() {
        let state = CONST.START_STATE;
        state = updateNode(state, this.tuple);
        const changed = state !== this.hash;
        this.hash = state;
        if (changed) {
            const tuple = this.tuple;
            const slice = PersistedSlice.deserialize(this.model, this.txt, this.chunk, tuple);
            this.stacking = slice.stacking;
            this.start = slice.start;
            this.end = slice.end;
        }
        return this.hash;
    }
    /** ----------------------------------------------------- {@link Printable} */
    toStringName() {
        const type = this.type();
        if (typeof type === 'number' && Math.abs(type) <= 64 && SliceTypeName[type]) {
            return `slice [${SliceStackingName[this.stacking]}] <${SliceTypeName[type]}>`;
        }
        return `slice [${SliceStackingName[this.stacking]}] ${JSON.stringify(type)}`;
    }
    toStringHeaderName() {
        const data = this.data();
        const dataFormatted = data ? prettyOneLine(data) : '∅';
        const dataLengthBreakpoint = 32;
        const header = `${this.toStringName()} ${super.toString('', true)}, ${SliceStackingName[this.stacking]}, ${JSON.stringify(this.type())}${dataFormatted.length < dataLengthBreakpoint ? `, ${dataFormatted}` : ''}`;
        return header;
    }
    toStringHeader(tab = '') {
        const data = this.data();
        const dataFormatted = data ? prettyOneLine(data) : '';
        const dataLengthBreakpoint = 32;
        return (this.toStringHeaderName() +
            printTree(tab, [dataFormatted.length < dataLengthBreakpoint ? null : (tab) => dataFormatted]));
    }
}
//# sourceMappingURL=PersistedSlice.js.map