export class WorkbookProjectionInterceptorService {
    engine;
    interceptors = [];
    registrationCounter = 0;
    constructor(engine) {
        this.engine = engine;
    }
    register(interceptor) {
        const registered = Object.freeze({
            ...interceptor,
            priority: interceptor.priority ?? 0,
            registrationOrder: this.registrationCounter,
        });
        this.registrationCounter += 1;
        this.interceptors.push(registered);
        this.sortInterceptors();
        return () => {
            const index = this.interceptors.indexOf(registered);
            if (index >= 0) {
                this.interceptors.splice(index, 1);
            }
        };
    }
    list(point) {
        return this.interceptors
            .filter((interceptor) => point === undefined || interceptor.point === point)
            .map(({ registrationOrder: _registrationOrder, projectRangeChrome: _projectRangeChrome, projectCommandMetadata: _projectCommandMetadata, ...registration }) => Object.freeze({ ...registration }));
    }
    rangeChrome(range) {
        return this.interceptors.flatMap((interceptor) => {
            if (interceptor.point !== 'rangeChrome' || !interceptor.projectRangeChrome) {
                return [];
            }
            return interceptor.projectRangeChrome({ range }, this.context(interceptor.featureId));
        });
    }
    commandMetadata(request) {
        return this.interceptors.flatMap((interceptor) => {
            if (interceptor.point !== 'commandMetadata' || !interceptor.projectCommandMetadata) {
                return [];
            }
            const projection = interceptor.projectCommandMetadata({ request }, this.context(interceptor.featureId));
            return projection ? [projection] : [];
        });
    }
    context(featureId) {
        return {
            engine: this.engine,
            featureId,
        };
    }
    sortInterceptors() {
        this.interceptors.sort((left, right) => {
            const priorityDelta = (right.priority ?? 0) - (left.priority ?? 0);
            return priorityDelta === 0 ? left.registrationOrder - right.registrationOrder : priorityDelta;
        });
    }
}
//# sourceMappingURL=workbook-projection-interceptors.js.map