"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDurationTimer = exports.getPerformance = exports.setPerformance = exports.ObjectIdentifier = void 0;
exports.createToken = createToken;
exports.isLocationArray = isLocationArray;
exports.isDefinitionLinkArray = isDefinitionLinkArray;
exports.score = score;
exports.targetsNotebooks = targetsNotebooks;
const tslib_1 = require("tslib");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const path_1 = require("@opensumi/ide-utils/lib/path");
const types = tslib_1.__importStar(require("../../../../common/vscode/ext-types"));
function createToken() {
    return Object.freeze({
        isCancellationRequested: false,
        onCancellationRequested: ide_core_common_1.Event.None,
    });
}
var ObjectIdentifier;
(function (ObjectIdentifier) {
    ObjectIdentifier.name = '$ident';
    function mixin(obj, id) {
        Object.defineProperty(obj, ObjectIdentifier.name, { value: id, enumerable: true });
        return obj;
    }
    ObjectIdentifier.mixin = mixin;
    function of(obj) {
        return obj[ObjectIdentifier.name];
    }
    ObjectIdentifier.of = of;
})(ObjectIdentifier || (exports.ObjectIdentifier = ObjectIdentifier = {}));
function isLocationArray(array) {
    return Array.isArray(array) && array.length > 0 && array[0] instanceof types.Location;
}
function isDefinitionLinkArray(array) {
    return (Array.isArray(array) &&
        array.length > 0 &&
        array[0].hasOwnProperty('targetUri') &&
        array[0].hasOwnProperty('targetRange'));
}
function score(selector, candidateUri, candidateLanguage, candidateIsSynchronized, candidateNotebookUri, candidateNotebookType) {
    if (Array.isArray(selector)) {
        let ret = 0;
        for (const filter of selector) {
            const value = score(filter, candidateUri, candidateLanguage, candidateIsSynchronized, candidateNotebookUri, candidateNotebookType);
            if (value === 10) {
                return value;
            }
            if (value > ret) {
                ret = value;
            }
        }
        return ret;
    }
    else if (typeof selector === 'string') {
        if (!candidateIsSynchronized) {
            return 0;
        }
        if (selector === '*') {
            return 5;
        }
        else if (selector === candidateLanguage) {
            return 10;
        }
        else {
            return 0;
        }
    }
    else if (selector) {
        const { language, pattern, scheme, hasAccessToAllModels, notebookType } = selector;
        if (!candidateIsSynchronized && !hasAccessToAllModels) {
            return 0;
        }
        if (notebookType && candidateNotebookUri) {
            candidateUri = candidateNotebookUri;
        }
        let result = 0;
        if (scheme) {
            if (scheme === candidateUri.scheme) {
                result = 10;
            }
            else if (scheme === '*') {
                result = 5;
            }
            else {
                return 0;
            }
        }
        if (language) {
            if (language === candidateLanguage) {
                result = 10;
            }
            else if (language === '*') {
                result = Math.max(result, 5);
            }
            else {
                return 0;
            }
        }
        if (notebookType) {
            if (notebookType === candidateNotebookType) {
                result = 10;
            }
            else if (notebookType === '*' && candidateNotebookType !== undefined) {
                result = Math.max(result, 5);
            }
            else {
                return 0;
            }
        }
        if (pattern) {
            let normalizedPattern;
            if (typeof pattern === 'string') {
                normalizedPattern = pattern;
            }
            else {
                normalizedPattern = { ...pattern, base: (0, path_1.normalize)(pattern.base) };
            }
            if (normalizedPattern === candidateUri.fsPath || (0, ide_core_common_1.match)(pattern, candidateUri.fsPath)) {
                result = 10;
            }
            else {
                return 0;
            }
        }
        return result;
    }
    else {
        return 0;
    }
}
let performance = null;
const setPerformance = (perf) => {
    performance = perf;
};
exports.setPerformance = setPerformance;
const getPerformance = () => performance;
exports.getPerformance = getPerformance;
/**
 *
 * @description 记录插件进程中相关操作的耗时
 */
const getDurationTimer = () => {
    const perf = (0, exports.getPerformance)();
    const recorder = perf ? perf : Date;
    const startTime = recorder.now();
    return {
        end: () => {
            const cost = Math.round(recorder.now() - startTime);
            return cost;
        },
    };
};
exports.getDurationTimer = getDurationTimer;
function targetsNotebooks(selector) {
    if (typeof selector === 'string') {
        return false;
    }
    else if (Array.isArray(selector)) {
        return selector.some(targetsNotebooks);
    }
    else {
        return !!selector.notebookType;
    }
}
//# sourceMappingURL=util.js.map