"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CursorState = void 0;
const sync_store_1 = require("../../../util/events/sync-store");
class CursorState {
    constructor(ctx) {
        this.ctx = ctx;
        /** Current score. */
        this.score = new sync_store_1.ValueSyncStore(0);
        /** By how much the score changed. */
        this.scoreDelta = new sync_store_1.ValueSyncStore(0);
        /** The last score that was shown to the user. */
        this.lastVisScore = new sync_store_1.ValueSyncStore(0);
    }
    /** -------------------------------------------------- {@link UiLifeCycles} */
    start() {
        const dom = this.ctx.dom;
        let lastNow = 0;
        const onChange = (event) => {
            const now = Date.now();
            const timeDiff = now - lastNow;
            let delta = 0;
            switch (event.detail.ev?.type) {
                case 'delete':
                case 'insert':
                case 'format':
                case 'marker': {
                    delta = timeDiff < 30 ? 10 : timeDiff < 70 ? 5 : timeDiff < 150 ? 2 : timeDiff <= 1000 ? 1 : -1;
                    break;
                }
                default: {
                    delta = timeDiff <= 1000 ? 0 : -1;
                    break;
                }
            }
            if (delta)
                this.score.next(delta >= 0 ? this.score.value + delta : 0);
            this.scoreDelta.next(delta);
            lastNow = now;
        };
        dom.et.addEventListener('change', onChange);
        return () => {
            dom.et.removeEventListener('change', onChange);
        };
    }
}
exports.CursorState = CursorState;
//# sourceMappingURL=state.js.map