"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDecorators = getDecorators;
exports.getNodeFirstDecoratorName = getNodeFirstDecoratorName;
exports.getNodeFirstDecoratorValue = getNodeFirstDecoratorValue;
exports.getDecoratorValues = getDecoratorValues;
exports.getSecurites = getSecurites;
exports.isDecorator = isDecorator;
exports.getPath = getPath;
exports.getProduces = getProduces;
const ts = __importStar(require("typescript"));
const initializer_value_1 = require("../metadataGeneration/initializer-value");
function tsHasDecorators(tsNamespace) {
    return typeof tsNamespace.canHaveDecorators === 'function';
}
function getDecorators(node, isMatching) {
    // beginning in ts4.8 node.decorator is undefined, use getDecorators instead.
    const decorators = tsHasDecorators(ts) && ts.canHaveDecorators(node) ? ts.getDecorators(node) : [];
    if (!decorators || !decorators.length) {
        return [];
    }
    return decorators
        .map((e) => {
        while (e.expression !== undefined) {
            e = e.expression;
        }
        return e;
    })
        .filter(isMatching);
}
function getNodeFirstDecoratorName(node, isMatching) {
    const decorators = getDecorators(node, isMatching);
    if (!decorators || !decorators.length) {
        return;
    }
    return decorators[0].text;
}
function getNodeFirstDecoratorValue(node, typeChecker, isMatching) {
    const decorators = getDecorators(node, isMatching);
    if (!decorators || !decorators.length) {
        return;
    }
    const values = getDecoratorValues(decorators[0], typeChecker);
    return values && values[0];
}
function getDecoratorValues(decorator, typeChecker) {
    const expression = decorator.parent;
    const expArguments = expression.arguments;
    if (!expArguments || !expArguments.length) {
        return [];
    }
    return expArguments.map(a => (0, initializer_value_1.getInitializerValue)(a, typeChecker));
}
function getSecurites(decorator, typeChecker) {
    const [first, second] = getDecoratorValues(decorator, typeChecker);
    if (isObject(first)) {
        return first;
    }
    return { [first]: second || [] };
}
function isDecorator(node, isMatching) {
    const decorators = getDecorators(node, isMatching);
    if (!decorators || !decorators.length) {
        return false;
    }
    return true;
}
function isObject(v) {
    return typeof v === 'object' && v !== null;
}
function getPath(decorator, typeChecker) {
    const [path] = getDecoratorValues(decorator, typeChecker);
    if (path === undefined) {
        return '';
    }
    return path;
}
function getProduces(node, typeChecker) {
    const producesDecorators = getDecorators(node, identifier => identifier.text === 'Produces');
    if (!producesDecorators || !producesDecorators.length) {
        return [];
    }
    return producesDecorators.map(decorator => getDecoratorValues(decorator, typeChecker)[0]);
}
//# sourceMappingURL=decoratorUtils.js.map