"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isObject = isObject;
exports.mixin = mixin;
exports.illegalArgument = illegalArgument;
exports.isLocationArray = isLocationArray;
exports.isDefinitionLinkArray = isDefinitionLinkArray;
exports.reviveRegExp = reviveRegExp;
exports.reviveIndentationRule = reviveIndentationRule;
exports.reviveOnEnterRule = reviveOnEnterRule;
exports.reviveOnEnterRules = reviveOnEnterRules;
exports.serializeEnterRules = serializeEnterRules;
exports.serializeRegExp = serializeRegExp;
exports.serializeIndentation = serializeIndentation;
exports.serializeAutoClosingPairs = serializeAutoClosingPairs;
const tslib_1 = require("tslib");
const types = tslib_1.__importStar(require("./ext-types"));
/**
 * Returns `true` if the parameter has type "object" and not null, an array, a regexp, a date.
 */
function isObject(obj) {
    return (typeof obj === 'object' && obj !== null && !Array.isArray(obj) && !(obj instanceof RegExp) && !(obj instanceof Date));
}
function mixin(destination, source, overwrite = true) {
    if (!isObject(destination)) {
        return source;
    }
    if (isObject(source)) {
        Object.keys(source).forEach((key) => {
            if (key in destination) {
                if (overwrite) {
                    if (isObject(destination[key]) && isObject(source[key])) {
                        mixin(destination[key], source[key], overwrite);
                    }
                    else {
                        destination[key] = source[key];
                    }
                }
            }
            else {
                destination[key] = source[key];
            }
        });
    }
    return destination;
}
function illegalArgument(message) {
    if (message) {
        return new Error(`Illegal argument: ${message}`);
    }
    else {
        return new Error('Illegal argument');
    }
}
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 &&
        // eslint-disable-next-line no-prototype-builtins
        array[0].hasOwnProperty('targetUri') &&
        // eslint-disable-next-line no-prototype-builtins
        array[0].hasOwnProperty('targetRange'));
}
function reviveRegExp(regExp) {
    if (typeof regExp === 'undefined' || regExp === null) {
        return undefined;
    }
    return new RegExp(regExp.pattern, regExp.flags);
}
function reviveIndentationRule(indentationRule) {
    if (typeof indentationRule === 'undefined' || indentationRule === null) {
        return undefined;
    }
    return {
        increaseIndentPattern: reviveRegExp(indentationRule.increaseIndentPattern),
        decreaseIndentPattern: reviveRegExp(indentationRule.decreaseIndentPattern),
        indentNextLinePattern: reviveRegExp(indentationRule.indentNextLinePattern),
        unIndentedLinePattern: reviveRegExp(indentationRule.unIndentedLinePattern),
    };
}
function reviveOnEnterRule(onEnterRule) {
    return {
        beforeText: reviveRegExp(onEnterRule.beforeText),
        afterText: reviveRegExp(onEnterRule.afterText),
        action: onEnterRule.action,
        previousLineText: reviveRegExp(onEnterRule.previousLineText),
    };
}
function reviveOnEnterRules(onEnterRules) {
    if (typeof onEnterRules === 'undefined' || onEnterRules === null) {
        return undefined;
    }
    return onEnterRules.map(reviveOnEnterRule);
}
function serializeEnterRules(rules) {
    if (typeof rules === 'undefined' || rules === null) {
        return undefined;
    }
    return rules.map((r) => ({
        action: r.action,
        beforeText: serializeRegExp(r.beforeText),
        afterText: serializeRegExp(r.afterText),
        previousLineText: serializeRegExp(r.previousLineText),
    }));
}
function serializeRegExp(regexp) {
    if (typeof regexp === 'undefined' || regexp === null) {
        return undefined;
    }
    return {
        pattern: regexp.source,
        flags: (regexp.global ? 'g' : '') + (regexp.ignoreCase ? 'i' : '') + (regexp.multiline ? 'm' : ''),
    };
}
function serializeIndentation(indentationRules) {
    if (typeof indentationRules === 'undefined' || indentationRules === null) {
        return undefined;
    }
    return {
        increaseIndentPattern: serializeRegExp(indentationRules.increaseIndentPattern),
        decreaseIndentPattern: serializeRegExp(indentationRules.decreaseIndentPattern),
        indentNextLinePattern: serializeRegExp(indentationRules.indentNextLinePattern),
        unIndentedLinePattern: serializeRegExp(indentationRules.unIndentedLinePattern),
    };
}
function serializeAutoClosingPairs(pairs) {
    if (!pairs) {
        return undefined;
    }
    return pairs.map((pair) => ({
        open: pair.open,
        close: pair.close,
        notIn: pair.notIn ? pair.notIn.map((tokenType) => types.SyntaxTokenType.toString(tokenType)) : undefined,
    }));
}
//# sourceMappingURL=utils.js.map