"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevToolsMarker = void 0;
exports.areAdaptableProfileTracksEnabled = areAdaptableProfileTracksEnabled;
exports.getMarker = getMarker;
const infinite_react_1 = require("@infinite-table/infinite-react");
const DevToolsTracks_1 = require("./DevToolsTracks");
const AVAILABLE_COLORS = [
    'primary',
    'primary-light',
    'primary-dark',
    'secondary',
    'secondary-light',
    'secondary-dark',
    'tertiary',
    'tertiary-light',
    'tertiary-dark',
    // 'error',
];
const colorMap = new Map();
function getColor(identifier) {
    if (colorMap.has(identifier)) {
        return colorMap.get(identifier);
    }
    const color = AVAILABLE_COLORS[colorMap.size % AVAILABLE_COLORS.length];
    colorMap.set(identifier, color);
    return color;
}
class DevToolsMarker {
    adaptableId;
    static create(adaptableId) {
        return new DevToolsMarker(adaptableId);
    }
    markerDetails = {
        label: '',
        track: '',
        trackGroup: undefined,
        color: undefined,
        details: [],
        tooltip: undefined,
    };
    stopped = false;
    constructor(adaptableId) {
        this.adaptableId = adaptableId;
        this.adaptableId = adaptableId;
    }
    start = (startDetails) => {
        if (this.markerDetails.startTs) {
            return this;
        }
        const start = performance.now();
        this.markerDetails.details = startDetails?.details ?? [];
        this.markerDetails.startTs = start;
        return this;
    };
    get startTimestamp() {
        return this.markerDetails.startTs;
    }
    get track() {
        const self = this;
        return Object.keys(DevToolsTracks_1.DevToolsTracks).reduce((acc, track) => {
            const trackObj = {
                start: self.start,
                end: (markerDetails) => {
                    return self.end({
                        ...markerDetails,
                        track: DevToolsTracks_1.DevToolsTracks[track].track,
                        label: markerDetails.label,
                    });
                },
                get label() {
                    const currentTrack = DevToolsTracks_1.DevToolsTracks[track];
                    return Object.keys(currentTrack.labels).reduce((labelAcc, label) => {
                        const labelObj = {
                            start: self.start,
                            end: (markerDetails) => {
                                return self.end({
                                    ...markerDetails,
                                    track: currentTrack.track,
                                    label: currentTrack.labels[label],
                                });
                            },
                        };
                        Object.defineProperty(labelAcc, label, {
                            get: () => {
                                self.markerDetails.label =
                                    currentTrack.labels[label];
                                return labelObj;
                            },
                        });
                        return labelAcc;
                    }, {});
                },
            };
            Object.defineProperty(acc, track, {
                get: () => {
                    const currentTrack = DevToolsTracks_1.DevToolsTracks[track];
                    self.markerDetails.track = currentTrack.track;
                    return trackObj;
                },
            });
            return acc;
        }, {});
    }
    end = (markerDetails = {}) => {
        if (this.stopped) {
            return this;
        }
        this.stopped = true;
        const start = markerDetails.startTs ?? this.markerDetails.startTs;
        const end = markerDetails.endTs ?? this.markerDetails.endTs ?? performance.now();
        let color = markerDetails.color || this.markerDetails.color || '';
        const trackGroup = markerDetails.trackGroup ||
            this.markerDetails.trackGroup ||
            `AdapTable (${this.adaptableId})`;
        const details = [...(this.markerDetails.details || []), ...(markerDetails.details || [])];
        const tooltip = markerDetails.tooltip || this.markerDetails.tooltip;
        const label = markerDetails.label || this.markerDetails.label || 'Unknown';
        const track = markerDetails.track || this.markerDetails.track || 'Unknown';
        if (!color) {
            const identifier = `${trackGroup}:${track}:${label}`;
            color = getColor(identifier);
        }
        performance.measure(label, {
            start,
            end,
            detail: {
                devtools: {
                    dataType: 'track-entry',
                    trackGroup,
                    track,
                    color: color || 'primary',
                    properties: details.length > 0 ? details.map((detail) => [detail.name, detail.value]) : undefined,
                    tooltipText: tooltip,
                },
            },
        });
        return this;
    };
}
exports.DevToolsMarker = DevToolsMarker;
function areAdaptableProfileTracksEnabled(adaptableId) {
    const trakcsSupported = typeof performance !== 'undefined' &&
        typeof performance.now === 'function' &&
        typeof performance.measure === 'function';
    if (!trakcsSupported) {
        return false;
    }
    if (typeof localStorage !== 'undefined' &&
        infinite_react_1.debug.isChannelEnabled(adaptableId ? `Adaptable:${adaptableId}:perf` : 'Adaptable:*:perf', typeof localStorage !== 'undefined' ? localStorage.debug || '' : '')) {
        return true;
    }
    if (typeof localStorage !== 'undefined' && localStorage.adaptableProfileTracks == 'true') {
        return true;
    }
    return false;
}
function getMarker(adaptableId) {
    const enabled = areAdaptableProfileTracksEnabled(adaptableId);
    const marker = DevToolsMarker.create(adaptableId);
    if (!enabled) {
        marker.start = () => marker;
        marker.end = () => marker;
    }
    return marker;
}
