"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constantsNode_1 = require("./constantsNode");
const DISABLE_OTHER_EXTENSIONS_FLAG = '--disable-extensions-except';
const LOAD_EXTENSION_FLAG = '--load-extension';
const DISABLE_EXTENSIONS_FLAG = '--disable-extensions';
const DISABLE_FEATURES = '--disable-features';
function mergeChromeArgs(userArgs, { disableOtherExtensions, disableLoadExtensionCommandFeature }) {
    let loadExtensionFlagIndex = -1;
    let disableExtensionsExceptFlagIndex = -1;
    let disableExtensionFlagIndex = -1;
    let disableFeaturesFlagIndex = -1;
    for (const [index, arg] of userArgs.entries()) {
        const hasLoadExtension = arg.includes(LOAD_EXTENSION_FLAG) || arg.includes('load-extension');
        const hasDisableExtensionsExcept = arg.includes(DISABLE_OTHER_EXTENSIONS_FLAG) ||
            arg.includes('disable-extensions-except');
        const hasDisableExtensions = (arg.includes(DISABLE_EXTENSIONS_FLAG) ||
            arg.includes('disable-extensions')) &&
            !hasDisableExtensionsExcept;
        const hasDisableFeatures = arg.includes(DISABLE_FEATURES) || arg.includes('disable-features');
        if (hasLoadExtension) {
            loadExtensionFlagIndex = index;
        }
        if (hasDisableExtensionsExcept) {
            disableExtensionsExceptFlagIndex = index;
        }
        if (hasDisableExtensions) {
            disableExtensionFlagIndex = index;
        }
        if (hasDisableFeatures) {
            disableFeaturesFlagIndex = index;
        }
    }
    if (loadExtensionFlagIndex !== -1) {
        userArgs[loadExtensionFlagIndex] += `,${constantsNode_1.PATH_TO_EXTENSION}`;
    }
    else {
        userArgs.push(`${LOAD_EXTENSION_FLAG}=${constantsNode_1.PATH_TO_EXTENSION}`);
    }
    if (disableExtensionsExceptFlagIndex !== -1) {
        userArgs[disableExtensionsExceptFlagIndex] += `,${constantsNode_1.PATH_TO_EXTENSION}`;
    }
    else if (disableOtherExtensions || disableExtensionFlagIndex !== -1) {
        userArgs.push(`${DISABLE_OTHER_EXTENSIONS_FLAG}=${constantsNode_1.PATH_TO_EXTENSION}`);
    }
    if (disableLoadExtensionCommandFeature) {
        if (disableFeaturesFlagIndex !== -1) {
            userArgs[disableFeaturesFlagIndex] +=
                `,DisableLoadExtensionCommandLineSwitch`;
        }
        else {
            userArgs.push(`${DISABLE_FEATURES}=DisableLoadExtensionCommandLineSwitch`);
        }
    }
    return userArgs;
}
exports.default = mergeChromeArgs;
//# sourceMappingURL=mergeChromeArgs.js.map