var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import ts from 'typescript';
import { getExternalSymbol } from './getExternalSymbol';
import t, { find } from './traverse';
var DocParser = /** @class */ (function () {
    function DocParser(program, plugins) {
        if (plugins === void 0) { plugins = []; }
        this.program = program;
        this.plugins = plugins;
        /**
         * This is the shared mutable instance of all exported symbols already processed. You can push new
         * symbols or search for existing symbols. If your plugin doesn't need to access existing symbols,
         * you can ignore this parameter.
         */
        this.symbols = [];
        this.checker = program.getTypeChecker();
    }
    /**
     * Get all {@link ExportedSymbol}s from a file.
     */
    DocParser.prototype.getExportedSymbols = function (fileName) {
        var _this = this;
        var symbols = [];
        var sourceFile = this.program.getSourceFile(fileName);
        if (!sourceFile)
            return symbols;
        find(sourceFile, function (node) {
            var kind = node.kind;
            return ([
                'VariableDeclaration',
                'InterfaceDeclaration',
                'TypeAliasDeclaration',
                'FunctionDeclaration',
                'EnumDeclaration',
                'ClassDeclaration',
            ]
                .map(function (k) { return ts.SyntaxKind[k]; })
                .includes(kind) && isNodeExported(_this.checker, node));
        }).forEach(function (node) {
            var symbol = getSymbolFromNode(_this.checker, node);
            var previousSymbolsLength = _this.symbols.length;
            if (symbol) {
                var exportedSymbol = __assign(__assign({ name: symbol.name, fileName: fileName }, findDocComment(_this.checker, symbol)), { type: _this.getValueFromNode(node) });
                symbols.push(exportedSymbol);
                var addedSymbolsLength = previousSymbolsLength - _this.symbols.length;
                // add all symbols added by the parser
                if (addedSymbolsLength) {
                    _this.symbols.slice(addedSymbolsLength).forEach(function (symbol) {
                        symbols.push(symbol);
                    });
                }
                _this.symbols.push(exportedSymbol);
            }
        });
        return symbols;
    };
    DocParser.prototype.getValueFromNode = function (node) {
        return getValueFromNode(this, node);
    };
    DocParser.prototype.getValueFromType = function (type, node) {
        return getValueFromType(this, type, node);
    };
    return DocParser;
}());
export { DocParser };
/**
 * This is the main recursive function for creating docs from the Typescript AST. The AST is
 * traversed and type information is extracted from source code. The Typescript type checker is used
 * to help traverse the AST by getting Symbols or Types and getting back source nodes.
 *
 * For example, if an `interface` is encountered:
 * - Start with a declaration - a Node
 * - Ask the type checker for the type information of the interface - returns a Type
 * - Get all the properties of the type - returns Symbols
 * - Iterate over property symbols and get a declaration - returns a Node
 * - Return a doc entry of the declaration node
 */
function getValueFromNode(parser, node) {
    if (node === undefined) {
        // This shouldn't happen, but we'd rather see `???` in the output than crash
        return unknownValue('???');
    }
    return (parser.plugins.reduce(function (result, fn) {
        return result || fn(node, parser);
    }, undefined) || _getValueFromNode(parser, node));
}
/**
 * Private recursing function. Doesn't include plugins.
 */
function _getValueFromNode(parser, node) {
    var _a, _b, _c, _d, _e, _f;
    var checker = parser.checker;
    // Uncomment for debugging
    // console.log(
    //   t.getKindNameFromNode(node) || node.kind,
    //   safeGetText(checker, node),
    //   checker.typeToString(checker.getTypeAtLocation(node))
    // );
    /**
     * A tuple type is an array with positional types.
     *
     * ```ts
     * type A = [string, number]
     * ```
     *
     * In this example, the TupleType is `[string, number]`. Each element in the
     * TupleType is its own node, so we map over them and recurse.
     */
    if (t.isTupleType(node)) {
        return {
            kind: 'tuple',
            value: node.elements.map(function (e) { return getValueFromNode(parser, e); }),
        };
    }
    /**
     * For the purpose of docs, the following are equivalent:
     *
     * ```ts
     * interface A<T> {
     *   B: T
     * }
     *
     * type A<T> = {
     *   B: T
     * }
     * ```
     *
     * The TypeAliasDeclaration has a `type` that is a `TypeLiteral`
     */
    if ((t.isTypeAliasDeclaration(node) && t.isTypeLiteral(node.type)) ||
        t.isInterfaceDeclaration(node)) {
        // Treat Interfaces and Types with TypeLiterals as interfaces
        var type = checker.getTypeAtLocation(node);
        var properties = type
            .getProperties()
            .map(function (p) {
            return getValueFromNode(parser, getValueDeclaration(p));
        })
            .filter(filterObjectProperties);
        // get index signature...
        var indexType = getIndexSignatureFromType(parser, type);
        if (isObject(type) && (properties.length || indexType)) {
            var typeParameters = (_a = (node.typeParameters ||
                node.parent.typeParameters)) === null || _a === void 0 ? void 0 : _a.map(function (p) { return getValueFromNode(parser, p); });
            return {
                kind: 'object',
                properties: properties,
                typeParameters: typeParameters,
                indexSignature: indexType,
            };
        }
    }
    /**
     * A declaration using the keyword `type`. For example:
     *
     * ```ts
     * type A = string
     * ```
     *
     * The TypeAliasDeclaration is everything in this example. The TypeAliasDeclaration can include
     * `typeParameters` which are the generics. In the following example, the `typeParameters` are an
     * array including the `T` inside the `<T>`:
     *
     * ```ts
     * type A<T> = T
     * ```
     */
    if (t.isTypeAliasDeclaration(node)) {
        var typeParameters = (_b = node.typeParameters) === null || _b === void 0 ? void 0 : _b.map(function (p) { return getValueFromNode(parser, p); });
        // We need to test if the `node.type` is a TypeReference and if that TypeReference is an exported
        // symbol. If the TypeReference is exported, we can continue with recursing on the `node.type`. If
        // it is not an exported symbol, we need to instead evaluate the type of the declaration directly using
        // the type checker. This prevents us from going down a rabbit hole of evaluating ASTs that are unhelpful
        // to documentation. For example:
        //
        // ```ts
        // type ValueOf<T> = T[keyof T];
        // type Foo = { a: 'first', b: 'second' }
        // export type Bar = ValueOf<Foo>
        // ```
        // If `ValueOf` was exported, the type would be documented as `ValueOf<{a: 'first', b:
        // 'second'}>`, but if it isn't exported, the value is `'first' | 'second'`
        var isLocalTypeReference = t.isTypeReference(node.type) && !isExportedSymbol(checker, node.type.typeName);
        var value = isLocalTypeReference
            ? getValueFromType(parser, checker.getTypeAtLocation(node), node) ||
                unknownValue(safeGetText(checker, node))
            : getValueFromNode(parser, node.type);
        return {
            kind: 'type',
            typeParameters: typeParameters || [],
            value: value,
        };
    }
    /**
     * We'll treat classes as objects with properties
     * ```ts
     * class A {}
     * ```
     */
    if (t.isClassDeclaration(node)) {
        var type = checker.getTypeAtLocation(node);
        var value = getObjectValueFromType(parser, type);
        if (value.kind === 'object') {
            value.typeParameters = (_c = node.typeParameters) === null || _c === void 0 ? void 0 : _c.map(function (p) {
                return getValueFromNode(parser, p);
            }).filter(filterObjectTypeParameters);
        }
        return value;
    }
    /**
     * A TypeLiteral is the object syntax of a `type`. It is similar to an interface. A
     * `TypeDeclaration` with a `TypeLiteral` is already checked higher up in this function, the only
     * `TypeLiteral` matching here are used anonymously.
     *
     * ```ts
     * type A = B & {
     *   C: 'c'
     * }
     * ```
     *
     * In this example, the TypeLiteral is `{ C: 'c' }`.
     */
    if (t.isTypeLiteral(node)) {
        var properties = node.members
            .map(function (member) {
            return getValueFromNode(parser, member);
        })
            .filter(filterObjectProperties);
        return { kind: 'object', properties: properties };
    }
    /**
     * A ArrayType is a special notation for arrays. For example:
     *
     * A[]
     *
     * An alternative would be `Array<A>`, but that would be a TypeReference
     * `Array` with a `typeArgument` of a TypeReference `A`
     */
    if (t.isArrayType(node)) {
        return { kind: 'array', value: getValueFromNode(parser, node.elementType) };
    }
    /**
     * A TypeParameter is a type version of a function parameter. It is the
     * generic types of another type.
     *
     * For example:
     *
     * type A<T> = string
     *
     * The TypeParameter is `T`. TypeParameters can have a constraint and a
     * default. For example:
     *
     * type A<T extends string = 'a'> = T
     *
     * The constraint is `string` and the default is `'a'`
     */
    if (t.isTypeParameter(node)) {
        var constraint = node.constraint ? getValueFromNode(parser, node.constraint) : undefined;
        var defaultValue = node.default ? getValueFromNode(parser, node.default) : undefined;
        return {
            kind: 'typeParameter',
            name: node.name.text,
            defaultValue: defaultValue,
            constraint: constraint,
            required: !defaultValue,
        };
    }
    /**
     * A MethodDeclaration is a type of property declaration within a JS object.
     * It is a special syntax for declaring a function property or method of an
     * object. This includes any JSDoc.
     *
     * For example:
     *
     * ```ts
     * const A = {
     *   onClick(e: Event) {}
     * }
     * ```
     *
     * In this example, the MethodDeclaration is `onClick(e: Event) {}`. Notice
     * there is no property signature like `onClick: (e: Event) => {}`
     *
     * Do before `ts.isFunctionLike` because we want to treat `MethodDeclaration`
     * as a `parameter` instead of a `function`
     */
    if (t.isMethodDeclaration(node)) {
        var signature = getValueFromSignatureNode(parser, node);
        var symbol_1 = getSymbolFromNode(checker, node);
        var type = checker.getTypeAtLocation(node);
        var jsDoc = findDocComment(checker, symbol_1);
        if (jsDoc.tags.default) {
        }
        return __assign({ kind: 'property', name: (symbol_1 === null || symbol_1 === void 0 ? void 0 : symbol_1.name) || '', type: signature, required: symbol_1 ? !isOptional(symbol_1) && !includesUndefined(type) : false }, jsDoc);
    }
    /**
     * A MethodSignature is a type of property declaration within a TS type. It is a special syntax
     * for declaring a function property or method of a type. This includes any JSDoc.
     *
     * For example:
     * ```ts
     * type A = {
     *   onClick(e: Event): void
     * }
     * ```
     *
     * In this example, the MethodSignature is the `onClick(e: Event): void`. An alternative might be
     * `onClick: (e: Event) => void`.
     *
     * Do before `ts.isFunctionLike` because we want to treat `MethodSignature` as a `member` instead
     * of a `function`.
     */
    if (t.isMethodSignature(node)) {
        var signature = getValueFromSignatureNode(parser, node);
        var symbol_2 = getSymbolFromNode(checker, node);
        var jsDoc = findDocComment(checker, symbol_2);
        return __assign({ kind: 'property', name: (symbol_2 === null || symbol_2 === void 0 ? void 0 : symbol_2.name) || node.name.text || '', type: signature }, jsDoc);
    }
    /**
     * A FunctionType is a function declaration in Typescript's type annotation
     */
    if (t.isFunctionType(node)) {
        var declaration = node;
        var signature = checker.getSignatureFromDeclaration(declaration);
        if (signature) {
            return getValueFromSignature(parser, node, signature);
        }
        else {
            return getValueFromSignature(parser, node, generateSignatureFromTypeNode(parser, node));
        }
    }
    // All function like have call signatures. All special function-like node
    // processing should happen before this line
    if (ts.isFunctionLike(node)) {
        return getValueFromSignatureNode(parser, node);
    }
    /**
     * A variable declaration is the inner declaration of any JS variable. Here's
     * the structure of a `VariableDeclaration`:
     *
     * VariableStatement VariableDeclarationList VariableDeclaration
     *
     * For example,
     *
     * export const A = 'a', B = 'b'
     *
     * There are 2 VariableDeclarations: `A = 'a'` and `B = 'b'`
     */
    if (t.isVariableDeclaration(node)) {
        // if the declaration already has a type node, return the value from the type node
        if (node.type) {
            return getValueFromNode(parser, node.type);
        }
        // An `AsExpression` is a type, so we'll return that
        if (node.initializer && t.isAsExpression(node.initializer)) {
            return getValueFromNode(parser, node.initializer);
        }
        if (node.initializer &&
            t.isIdentifier(node.initializer) &&
            isExportedSymbol(checker, node.initializer)) {
            var symbol_3 = getSymbolFromNode(checker, node.initializer);
            return {
                kind: 'symbol',
                name: node.initializer.text,
                fileName: (_d = symbol_3 === null || symbol_3 === void 0 ? void 0 : symbol_3.valueDeclaration) === null || _d === void 0 ? void 0 : _d.getSourceFile().fileName,
            };
        }
        if (node.initializer && ts.isFunctionLike(node.initializer)) {
            return getValueFromNode(parser, node.initializer);
        }
        // We have no type information in the AST. We'll get the Type from the type checker and run some
        // tests on what we have
        var type = checker.getTypeAtLocation(node.initializer || node);
        // Both functions and objects are considered objects to Typescript
        if (isObject(type)) {
            if (type.objectFlags & ts.ObjectFlags.ArrayLiteral) {
                return getValueFromType(parser, type) || unknownValue(safeGetText(checker, node));
            }
            return getObjectValueFromType(parser, type);
        }
        var value = getValueFromType(parser, type);
        if (value)
            return value;
    }
    /**
     * A property signature is a property of a type object (or interface). This will include JSDoc.
     * In the below example, the property signature is `B: string`
     *
     * type A = {
     *   B: string
     * }
     */
    if (t.isPropertySignature(node)) {
        var symbol_4 = getSymbolFromNode(checker, node);
        var jsDoc = findDocComment(checker, symbol_4);
        // Get the name of the property - it could be a symbol or have a `name` that is an identifier or string literal
        var name_1 = (symbol_4 === null || symbol_4 === void 0 ? void 0 : symbol_4.name) ||
            (t.isIdentifier(node.name)
                ? node.name.text
                : t.isStringLiteral(node.name)
                    ? node.name.text
                    : '');
        return __assign({ kind: 'property', name: name_1, required: node.questionToken ? false : true, type: node.type
                ? getValueFromNode(parser, node.type)
                : unknownValue(safeGetText(checker, node)) }, jsDoc);
    }
    /**
     * A PropertyDeclaration is a property declared in a class
     *
     * ```ts
     * class A {
     *   a = 'b'
     * }
     * ```
     *
     * In this example, the PropertyDeclaration is `a = 'b'`
     */
    if (t.isPropertyDeclaration(node)) {
        var name_2 = getNodeName(node);
        var symbol_5 = getSymbolFromNode(checker, node);
        var jsDoc = findDocComment(checker, symbol_5);
        var type = checker.getTypeAtLocation(node);
        return __assign({ kind: 'property', name: name_2 || (symbol_5 === null || symbol_5 === void 0 ? void 0 : symbol_5.name) || 'unknown', type: getValueFromType(parser, type) || unknownValue(safeGetText(checker, node)) }, jsDoc);
    }
    /**
     * An ObjectLiteralExpression is a JS value of an object literal.
     *
     * For example:
     * ```ts
     * const a = {
     *   b: 'b'
     * }
     * ```
     *
     * In this example, `{ b, 'b' }` is the full object literal (including newlines)
     */
    if (t.isObjectLiteralExpression(node)) {
        return {
            kind: 'object',
            properties: node.properties
                .flatMap(function (property) {
                var value = getValueFromNode(parser, property);
                if (value.kind === 'object') {
                    return value.properties;
                }
                return value;
            })
                .filter(filterObjectProperties),
        };
    }
    if (t.isInferType(node)) {
        return {
            kind: 'infer',
            value: getValueFromNode(parser, node.typeParameter),
        };
    }
    if (t.isSpreadAssignment(node)) {
        var symbol_6 = getSymbolFromNode(checker, node.expression);
        var declaration = getValueDeclaration(symbol_6);
        if (declaration) {
            return getValueFromNode(parser, declaration);
        }
    }
    if (t.isArrayLiteralExpression(node)) {
        var values_1 = [];
        node.elements.forEach(function (element) {
            if (t.isSpreadElement(element)) {
                t.getKindNameFromNode(element);
                var value = getValueFromNode(parser, element);
                if (value.kind === 'array' || value.kind === 'tuple') {
                    values_1 = values_1.concat(value.value);
                }
            }
            else {
                var value = getValueFromNode(parser, element);
                values_1.push(value);
            }
        });
        return {
            kind: 'tuple',
            value: values_1,
        };
    }
    if (t.isSpreadElement(node)) {
        var symbol_7 = getSymbolFromNode(checker, node.expression);
        var declaration = getValueDeclaration(symbol_7);
        if (declaration) {
            return getValueFromNode(parser, declaration);
        }
        return getValueFromNode(parser, node.expression);
    }
    /**
     * A property assignment is part of a object value. This will include JSDocs.
     * A property assignment is like a property signature, but with values instead of types.
     * In the below example, the property assignment is `B: 'b'`.
     *
     * const A = {
     *   B: 'b'
     * }
     *
     * */
    if (t.isPropertyAssignment(node)) {
        var type = checker.getTypeAtLocation(node);
        var symbol_8 = getSymbolFromNode(checker, node);
        var jsDoc = findDocComment(checker, symbol_8);
        // For default values, we want the value and not the type. An `AsExpression` redirects to types,
        // so we want to bypass it to get the value node
        var defaultValueNode = t.isAsExpression(node.initializer)
            ? node.initializer.expression
            : node.initializer;
        return __assign({ kind: 'property', name: (symbol_8 === null || symbol_8 === void 0 ? void 0 : symbol_8.name) || '', defaultValue: getValueFromNode(parser, defaultValueNode), type: getValueFromType(parser, type) || unknownValue(safeGetText(checker, node)), required: symbol_8 ? !isOptional(symbol_8) && !includesUndefined(type) : false }, jsDoc);
    }
    // as A
    if (t.isAsExpression(node)) {
        if (safeGetText(checker, node) === 'const') {
            var type = checker.getTypeAtLocation(node.parent);
            return getValueFromType(parser, type) || unknownValue(safeGetText(checker, node));
        }
        return getValueFromNode(parser, node.type);
    }
    if (t.isTypeOperator(node) && node.operator === ts.SyntaxKind.KeyOfKeyword) {
        // We can get into trouble if `node` is a synthetic node. We'll check if we're encountering
        // something like `keyof A`. In this case, we'll get the symbol and ask for the properties of
        // the symbol's declaration.
        if (t.isTypeReference(node.type)) {
            var symbol_9 = getSymbolFromNode(checker, node.type.typeName);
            var declaration = getValueDeclaration(symbol_9);
            if (symbol_9 && declaration && isExportedSymbol(checker, declaration)) {
                return {
                    kind: 'keyof',
                    name: {
                        kind: 'symbol',
                        name: symbol_9.name,
                        fileName: declaration.getSourceFile().fileName,
                        value: "keyof " + symbol_9.name,
                    },
                };
            }
        }
        // A `keyof` in a synthetic TypeNode will cause us problems. It means a TypeNode was generated
        // by `getValueFromType`. `checker.getTypeAtLocation` on a synthetic TypeNode will always be
        // `any`. I means going down this tree will not work and we need to throw an error. This error
        // will bubble to a `try...catch` in `getValueFromType` and it will instead extract a value from
        // the `Type` and not the `TypeNode`
        if (node.end < 0) {
            // We're a synthetic TypeNode
            throw Error('Cannot process a synthetic TypeNode with a `keyof`');
        }
        return (getValueFromType(parser, checker.getTypeFromTypeNode(node), node) ||
            unknownValue(safeGetText(checker, node)));
    }
    // A literal type contains literals like `string` or `number` or `'foo'`
    if (t.isLiteralType(node)) {
        return getValueFromNode(parser, node.literal);
    }
    // true
    if (node.kind === ts.SyntaxKind.TrueKeyword) {
        return { kind: 'boolean', value: true };
    }
    // false
    if (node.kind === ts.SyntaxKind.FalseKeyword) {
        return { kind: 'boolean', value: false };
    }
    // string
    if (node.kind === ts.SyntaxKind.StringKeyword) {
        return { kind: 'primitive', value: 'string' };
    }
    // number
    if (node.kind === ts.SyntaxKind.NumberKeyword) {
        return { kind: 'primitive', value: 'number' };
    }
    // boolean
    if (node.kind === ts.SyntaxKind.BooleanKeyword) {
        return { kind: 'primitive', value: 'boolean' };
    }
    // 'a'
    if (t.isStringLiteral(node)) {
        return { kind: 'string', value: node.text };
    }
    // null
    if (t.isNullKeyword(node)) {
        return { kind: 'primitive', value: 'null' };
    }
    if (node.kind === ts.SyntaxKind.NeverKeyword) {
        return { kind: 'primitive', value: 'never' };
    }
    // 100
    if (t.isNumericLiteral(node)) {
        return { kind: 'number', value: Number(node.text) };
    }
    // void
    if (node.kind === ts.SyntaxKind.VoidKeyword) {
        return { kind: 'primitive', value: 'void' };
    }
    // any
    if (node.kind === ts.SyntaxKind.AnyKeyword) {
        return { kind: 'primitive', value: 'any' };
    }
    // unknown
    if (node.kind === ts.SyntaxKind.UnknownKeyword) {
        return { kind: 'primitive', value: 'unknown' };
    }
    // undefined
    if (node.kind === ts.SyntaxKind.UndefinedKeyword) {
        return { kind: 'primitive', value: 'undefined' };
    }
    // `something`
    if (t.isTemplateExpression(node)) {
        return { kind: 'primitive', value: 'string' };
    }
    // type A = `{anything}`
    if (t.isTemplateLiteralType(node)) {
        var type = checker.getTypeAtLocation(node);
        return getValueFromType(parser, type, node) || unknownValue(checker.typeToString(type));
    }
    // A | B
    if (t.isUnionType(node)) {
        return {
            kind: 'union',
            value: node.types.map(function (type) { return getValueFromNode(parser, type); }),
        };
    }
    // A & B
    if (t.isIntersectionType(node)) {
        return {
            kind: 'intersection',
            value: node.types.map(function (type) { return getValueFromNode(parser, type); }),
        };
    }
    // ()
    if (t.isParenthesizedType(node)) {
        return { kind: 'parenthesis', value: getValueFromNode(parser, node.type) };
    }
    // type A = B['C']
    if (t.isIndexedAccessType(node)) {
        var type = checker.getTypeAtLocation(node);
        return getValueFromType(parser, type, node) || unknownValue(safeGetText(checker, node));
    }
    /**
     * A ConditionalType is a type-based ternary. For example:
     *
     * ```ts
     * type A<T> = T extends string ? true : false
     * ```
     *
     * In this example, the ConditionalType is `T extends string ? true : false`.
     * The following properties of the node are as follows from the example:
     * - `checkType`: `T`
     * - `extendsType`: `string`
     * - `trueType`: `true`
     * - `falseType`: `false`
     */
    if (t.isConditionalType(node)) {
        return {
            kind: 'conditional',
            check: getValueFromNode(parser, node.checkType),
            extends: getValueFromNode(parser, node.extendsType),
            trueType: getValueFromNode(parser, node.trueType),
            falseType: getValueFromNode(parser, node.falseType),
        };
    }
    /**
     * A QualifiedName is a type-base dot property access. It is used to access properties of a
     * namespace.
     *
     * ```ts
     * A.B
     * ```
     *
     * In this example, the namespace is `A` while the property of the namespace is `B`. Typescript
     * doesn't treat property access and index access as interchangeable. For example, `A.B` is not
     * the same this as `A['B']` in Typescript. The former is only allowed on namespaces while the
     * latter is only allowed for everything else. For example, accessing a property of an interface.
     */
    if (t.isQualifiedName(node)) {
        if (isExportedSymbol(checker, node.left)) {
            var value = checker.typeToString(checker.getTypeAtLocation(node.left));
            return {
                kind: 'qualifiedName',
                left: { kind: 'symbol', name: safeGetText(checker, node.left), value: value },
                right: { kind: 'string', value: safeGetText(checker, node.right) },
            };
        }
        // if the node.left is not exported, we'll reduce to a type
        var type = checker.getTypeAtLocation(node);
        return getValueFromType(parser, type, node) || unknownValue(checker.typeToString(type));
    }
    /**
     * A TypeQuery is the `typeof` keyword that instructs Typescript to extract the type of a value.
     * For example:
     *
     * ```ts
     * type A = typeof a
     * ```
     *
     * In this example, the TypeQuery is `typeof a`
     */
    if (t.isTypeQuery(node)) {
        if (isExportedSymbol(checker, node.exprName)) {
            var value = checker.typeToString(checker.getTypeAtLocation(node.exprName));
            return { kind: 'symbol', name: node.exprName.getText(), value: value };
        }
        var symbol_10 = getSymbolFromNode(checker, node.exprName);
        if (symbol_10) {
            var declaration = getValueDeclaration(symbol_10);
            return getValueFromNode(parser, declaration);
        }
    }
    /**
     * A PropertyAccessExpression is an expression with a property on it.
     *
     * For example:
     * ```ts
     * foo.bar = 'bar'
     * ```
     *
     * In this example, the `PropertyAccessExpression` is `foo.bar`. It can be used by functions to
     * add additional properties on the function.
     */
    if (t.isPropertyAccessExpression(node)) {
        var typeInfo = void 0;
        if (t.isAsExpression(node.name)) {
            typeInfo = getValueFromNode(parser, node.name);
        }
        var type = checker.getTypeAtLocation(node);
        typeInfo = getValueFromType(parser, type) || unknownValue(checker.typeToString(type));
        return { kind: 'property', name: node.name.getText(), type: typeInfo };
    }
    /**
     * A TypeReference is a node that references a Typescript type rather than a JavaScript value.
     *
     * In the following example, `MyType` is a TypeReference (the declaration of MyType is omitted)
     * ```ts
     * const a = 'a' as MyType
     * const b: MyType = 'b'
     * type A = MyType
     * type B = Record<MyType>
     * ```
     *
     * Any time a type is referenced (not declared) is a TypeReference.
     * ```
     */
    if (t.isTypeReference(node)) {
        // handle `as const` specially. If we don't do this, we'll get into an infinite loop
        if (safeGetText(checker, node) === 'const') {
            var type_1 = checker.getTypeAtLocation(node.parent.parent);
            return getValueFromType(parser, type_1) || unknownValue(node.parent.parent.getText());
        }
        var typeParameters = (_e = node.typeArguments) === null || _e === void 0 ? void 0 : _e.map(function (p) { return getValueFromNode(parser, p); });
        var symbolNode = t.isQualifiedName(node.typeName) ? node.typeName.right : node.typeName;
        var symbol_11 = getSymbolFromNode(checker, symbolNode);
        var fileName = (_f = getValueDeclaration(symbol_11)) === null || _f === void 0 ? void 0 : _f.getSourceFile().fileName;
        var externalSymbol = getExternalSymbol((symbol_11 === null || symbol_11 === void 0 ? void 0 : symbol_11.name) || safeGetText(checker, node), fileName);
        if (externalSymbol) {
            return {
                kind: 'external',
                name: (symbol_11 === null || symbol_11 === void 0 ? void 0 : symbol_11.name) || '',
                url: externalSymbol,
                typeParameters: typeParameters,
            };
        }
        if (isExportedSymbol(checker, symbolNode)) {
            var value = checker.typeToString(checker.getTypeAtLocation(node.typeName));
            return { kind: 'symbol', name: safeGetText(checker, node.typeName), typeParameters: typeParameters, value: value };
        }
        // If it is a qualified name, handle that specially. The `left` might be a symbol
        if (t.isQualifiedName(node.typeName)) {
            return getValueFromNode(parser, node.typeName);
        }
        // The TypeReference isn't exported, so we'll return the type of the
        // symbol's value declaration directly
        var type = checker.getTypeAtLocation(node);
        if (symbol_11) {
            if (type.getFlags() & ts.TypeFlags.Instantiable) {
                // It is a generic type
                return { kind: 'generic', name: symbol_11 === null || symbol_11 === void 0 ? void 0 : symbol_11.name };
            }
            var declaration = getValueDeclaration(symbol_11);
            if (declaration) {
                var typeInfo = getValueFromNode(parser, declaration);
                // we want to embed objects
                if (typeInfo.kind === 'object') {
                    return typeInfo;
                }
            }
        }
        // The type reference is not external, not an exported symbol, and not generic.
        // Fall back to returning the value from it's type property
        return getValueFromType(parser, type) || unknownValue(safeGetText(checker, node));
    }
    /**
     * A ShorthandPropertyAssignment is a PropertyAssignment that is shorthanded where the `name` and
     * `initializer` are the same value.
     *
     * For example:
     * ```ts
     * const a = {
     *   b
     * }
     * ```
     *
     * In this example, `b` is the `ShorthandPropertyAssignment`. `b` is both the `name` and
     * `initializer` of the PropertyAssignment.
     *
     * Note the symbol declaration is the PropertyAssignment and not the symbol of the initializer. In
     * a PropertyAssignment, there are two parts, the `name` (name of the property) and an
     * `initializer` (the value of the property). In a PropertyAssignment, the `initializer` symbol
     * points to the VariableDeclaration.
     */
    if (t.isShorthandPropertyAssignment(node)) {
        var type = checker.getTypeAtLocation(node);
        var symbol_12 = getSymbolFromNode(checker, node);
        var jsDoc = findDocComment(checker, symbol_12);
        // see if the declaration is assigned to something exported
        return __assign({ kind: 'property', name: (symbol_12 === null || symbol_12 === void 0 ? void 0 : symbol_12.name) || '', type: getValueFromType(parser, type) || unknownValue(safeGetText(checker, node)) }, jsDoc);
    }
    /**
     * Declaration of a enum
     *
     * For example:
     * ```ts
     * enum A {
     *   a = 'a',
     *   b = 'b'
     * }
     * ```
     */
    if (t.isEnumDeclaration(node)) {
        return {
            kind: 'object',
            typeParameters: [],
            properties: node.members.map(function (m, index) {
                return {
                    kind: 'property',
                    name: safeGetText(checker, m.name),
                    type: m.initializer
                        ? getValueFromNode(parser, m.initializer)
                        : { kind: 'number', value: index },
                };
            }),
            callSignatures: [],
        };
    }
    /**
     * An Identifier isn't normally a node type we deal with, but it is useful for determining
     * defaultValue of things. If the initializer of a property is an identifier and that identifier
     * is an exported symbol, we'll assign it a symbol.
     *
     * For example:
     * ```ts
     * const a = b
     * ```
     *
     * In this example, `b` is the identifier because it is a value reference to something else.
     */
    if (t.isIdentifier(node)) {
        if (isExportedSymbol(checker, node)) {
            var symbol_13 = getSymbolFromNode(checker, node);
            var declaration = getValueDeclaration(symbol_13);
            return {
                kind: 'symbol',
                name: node.text,
                fileName: declaration === null || declaration === void 0 ? void 0 : declaration.getSourceFile().fileName,
                value: node.text,
            };
        }
        if (node.text === 'undefined') {
            return { kind: 'primitive', value: 'undefined' };
        }
    }
    /**
     *
     */
    if (t.isParameter(node)) {
        var type = checker.getTypeAtLocation(node);
        var symbol_14 = getSymbolFromNode(checker, node);
        var jsDoc = findDocComment(checker, symbol_14);
        var isRequired = node.questionToken
            ? false
            : node.initializer
                ? false
                : symbol_14
                    ? !isOptional(symbol_14) && !includesUndefined(type)
                    : false;
        var typeInfo = node.type
            ? getValueFromNode(parser, node.type)
            : getValueFromType(parser, type) || unknownValue(safeGetText(checker, node));
        var defaultValue = node.initializer ? getValueFromNode(parser, node.initializer) : undefined;
        /**
         * Set default values if an object binding pattern is found. We do this at the Parameter level,
         * because we have all the info here.
         *
         * For example:
         * ```ts
         * function A({ a = 'a', b}: Params)
         * ```
         *
         * In this example, the ObjectBindingPattern is `{ a = 'a', b }`
         */
        if (t.isObjectBindingPattern(node.name)) {
            var defaults_1 = getDefaultsFromObjectBindingParameter(parser, node);
            if (typeInfo.kind === 'object') {
                typeInfo.properties.forEach(function (p) {
                    if (!p.defaultValue && defaults_1[p.name]) {
                        p.defaultValue = defaults_1[p.name];
                    }
                });
            }
        }
        return __assign({ kind: 'parameter', name: (symbol_14 === null || symbol_14 === void 0 ? void 0 : symbol_14.name) || '', defaultValue: defaultValue, type: typeInfo, required: isRequired, rest: !!node.dotDotDotToken }, jsDoc);
    }
    /**
     * A call expression is an expression calling a function. In this case, we want to get the signature
     * and get a type for the return type
     */
    if (t.isCallExpression(node)) {
        var type = checker.getTypeAtLocation(node);
        var value = getValueFromType(parser, type);
        if (value) {
            return value;
        }
    }
    var symbol = getSymbolFromNode(checker, node);
    if (!symbol) {
        return unknownValue(safeGetText(checker, node));
    }
    return unknownValue(safeGetText(checker, node));
}
/** True if this is visible outside this file, false otherwise */
function isNodeExported(checker, node) {
    return (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export) !== 0;
}
export var defaultJSDoc = {
    description: '',
    tags: {},
    declarations: [],
};
export function getFullJsDocComment(checker, symbol) {
    var _a;
    if (symbol.getDocumentationComment === undefined) {
        return defaultJSDoc;
    }
    var mainComment = ts.displayPartsToString(symbol.getDocumentationComment(checker));
    if (mainComment) {
        mainComment = mainComment.replace(/\r\n/g, '\n');
    }
    var tags = symbol.getJsDocTags() || [];
    var tagMap = {};
    tags.forEach(function (tag) {
        var trimmedText = (tag.text || '').trim();
        var currentValue = tagMap[tag.name];
        tagMap[tag.name] = currentValue ? currentValue + '\n' + trimmedText : trimmedText;
    });
    return {
        description: mainComment,
        declarations: ((_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) !== null && _a !== void 0 ? _a : []).map(function (d) { return ({
            name: (symbol === null || symbol === void 0 ? void 0 : symbol.name) || '',
            filePath: d.getSourceFile().fileName,
        }); }),
        tags: tagMap,
    };
}
export function findDocComment(checker, symbol) {
    if (symbol) {
        var comment_1 = getFullJsDocComment(checker, symbol);
        if (comment_1.description || comment_1.declarations.length || comment_1.tags.default) {
            return comment_1;
        }
        var rootSymbols = checker.getRootSymbols(symbol);
        var commentsOnRootSymbols = rootSymbols
            .filter(function (x) { return x !== symbol; })
            .map(function (x) { return getFullJsDocComment(checker, x); })
            .filter(function (x) { return !!x.description || !!comment_1.tags.default; });
        if (commentsOnRootSymbols.length) {
            return commentsOnRootSymbols[0];
        }
    }
    return defaultJSDoc;
}
/**
 * Attempt to get the name of a declaration or expression
 */
export function getNodeName(node) {
    var name = ts.getNameOfDeclaration(node);
    if (name && 'text' in name) {
        return name.text;
    }
    return;
}
export function filterObjectProperties(value) {
    return value.kind === 'property';
}
export function filterObjectTypeParameters(value) {
    return value.kind === 'typeParameter';
}
export function filterFunctionParameters(value) {
    return value.kind === 'parameter';
}
// https://github.com/dsherret/ts-ast-viewer/blob/c71e238123d972bae889b3829e23b44f39d8d5c2/site/src/components/PropertiesViewer.tsx#L172
export function getSymbolFromNode(checker, node) {
    return node.symbol || checker.getSymbolAtLocation(node);
}
export function isObject(type) {
    return !!(type.flags & ts.TypeFlags.Object);
}
function includesUndefined(type) {
    return type.isUnion() ? type.types.some(function (t) { return t.flags & ts.TypeFlags.Undefined; }) : false;
}
function isTupleType(type) {
    var _a;
    return !!(((_a = type.target) === null || _a === void 0 ? void 0 : _a.objectFlags) & ts.ObjectFlags.Tuple);
}
/**
 * Given a node, extract a default value and only return a `Value` if we consider it to be
 * a valid default. For example, a literal is valid.
 *
 * Valid:
 * - `'medium'`
 * - `true`
 * - 10
 *
 * Invalid:
 * - `string`
 * - `{foo: string}`
 */
export function getValidDefaultFromNode(parser, node) {
    if (t.isFalseKeyword(node) ||
        t.isTrueKeyword(node) ||
        t.isLiteralType(node) ||
        t.isStringLiteral(node) ||
        t.isNumericLiteral(node)) {
        return parser.getValueFromNode(node);
    }
    return undefined;
}
/**
 * A parameter might represent a `ObjectBindingPattern` which can be used to set defaults. This will
 * return all defaults found within the `ObjectBindingPattern` and return them as a map of the
 * property name to the `Value`. These defaults can be used to piece together a default. Also
 * `getDefaultFromTags` can be used to get defaults from JSDoc tags.
 */
export function getDefaultsFromObjectBindingParameter(parser, node) {
    if (t.isObjectBindingPattern(node.name)) {
        return node.name.elements.reduce(function (result, element) {
            if (t.isBindingElement(element) && t.isIdentifier(element.name) && element.initializer) {
                var defaultValue = getValidDefaultFromNode(parser, element.initializer);
                if (defaultValue) {
                    result[element.name.text] = defaultValue;
                }
            }
            return result;
        }, {});
    }
    return {};
}
/**
 * An index signature is like a "leftover" of an object. For example:
 *
 * ```ts
 * interface A {
 *   a: string,
 *   b: string,
 *   [key: string]: string
 * }
 * ```
 *
 * The index signature is `[key: string]: string`. It allows an interface to specify valid
 * additional properties even though the interface has specific properties defined. Index signature
 * types depend on the Typescript version, so this function will have to be updated to support the
 * correct version of Typescript.
 */
function getIndexSignatureFromType(parser, type) {
    var _a;
    var checker = parser.checker;
    var indexSignature = checker.getIndexInfoOfType(type, ts.IndexKind.String) ||
        checker.getIndexInfoOfType(type, ts.IndexKind.Number);
    if (indexSignature) {
        var parameter = (_a = indexSignature.declaration) === null || _a === void 0 ? void 0 : _a.parameters[0];
        return {
            kind: 'indexSignature',
            name: (parameter === null || parameter === void 0 ? void 0 : parameter.name) ? safeGetText(checker, parameter === null || parameter === void 0 ? void 0 : parameter.name) : '',
            type: (parameter && getValueFromType(parser, checker.getTypeAtLocation(parameter), parameter)) ||
                unknownValue(''),
            value: indexSignature.declaration
                ? getValueFromNode(parser, indexSignature.declaration.type)
                : unknownValue(checker.typeToString(type)),
        };
    }
    return;
}
/**
 *
 * @param checker The shared Typescript checker
 * @param type The type we're trying to find a Value for
 * @param node An optional node that was used to generate the Type. It should be used for all type
 * nodes (AST nodes that are types and not JS values). This extra information can prevent errors and
 * infinite loops.
 */
export function getValueFromType(parser, type, node) {
    var checker = parser.checker;
    var originalNodeKind = node === null || node === void 0 ? void 0 : node.kind;
    var typeToString = checker.typeToString(type);
    // If the type is `any`, we want to bail now
    if (type.flags & ts.TypeFlags.Any) {
        return { kind: 'primitive', value: 'any' };
    }
    // check if the node is an external symbol
    // TODO: This won't work if the symbol contains a generic
    var externalSymbol = getExternalSymbol(typeToString);
    if (externalSymbol) {
        return { kind: 'external', name: typeToString, url: externalSymbol };
    }
    if (isTupleType(type)) {
        return {
            kind: 'union',
            value: checker.getTypeArguments(type).map(function (t) { return getValueFromType(parser, t) || unknownValue(''); }) ||
                [],
        };
    }
    // See if there is a TypeNode associated with the type. This is common in type definitions and can
    // be useful to get type parameters (for example, `Promise<boolean>`, `boolean` is a type
    // parameter). I'm using Signatures to get a return `Type`, but it might be next to impossible
    // to get a real `Node` out of it again. The `Type` of this node is `any` making it difficult.
    // For example, I might get a `BooleanKeyword` syntax kind, but I don't have guards for that type.
    //
    // A TypeNode is a synthetic node that doesn't have any associated source code. A TypeNode can
    // contain child nodes that are linked to real AST nodes in the source code. Think of a TypeNode
    // as an AST representation of `checker.typeToString()`. The string version is reduced to a string
    // of characters where a TypeNode is an AST representation of that string.
    //
    // One interesting fact about this is when you have a union type that overflows. You may see
    // something like:
    // ```ts
    // "'a' | 'b' | ...23 more... | 'z'"
    // ```
    // In this case, the TypeNode will be a UnionType that contains an Identifier node with a name of
    // "...23 more..."
    //
    // We generally prefer TypeNodes because I'm pretty sure it is what the Typescript language
    // service uses for the tooltips when hovering over text in your IDE. We try to go from Type to
    // Node whenever possible, but there are some cases where interacting directly with the types is
    // preferred. The union example is one of such examples.
    var typeNode = checker.typeToTypeNode(type, node, ts.NodeBuilderFlags.NoTruncation);
    // We try to extract useful type information from the TypeNode and go back to recursing the AST.
    // But, if the typeNode and original node have the same kind, we've actually lost information and
    // should skip processing the TypeNode.
    if (typeNode && originalNodeKind !== typeNode.kind) {
        // find the symbol
        if (t.isTypeReference(typeNode)) {
            var symbol = getSymbolFromNode(checker, typeNode.typeName);
            var declaration = getValueDeclaration(symbol);
            var fileName = declaration === null || declaration === void 0 ? void 0 : declaration.getSourceFile().fileName;
            if (symbol) {
                var externalSymbol_1 = getExternalSymbol(symbol.name, fileName);
                if (externalSymbol_1) {
                    return { kind: 'external', name: symbol.name, url: externalSymbol_1 };
                }
                if (declaration && declaration !== node) {
                    if (isExportedSymbol(checker, declaration)) {
                        return { kind: 'symbol', name: symbol.name, value: typeToString };
                    }
                    return getValueFromNode(parser, declaration);
                }
            }
        }
        // Figure out if we should recurse back into AST nodes with our synthetic TypeNode. There are
        // exceptions to using the TypeNode. Exceptions where we actually lose type information. Some
        // examples are large unions or `keyof`:
        var exceptions = false;
        // The typeNode is a UnionType, but the number of items has overflowed with an identifier `...
        // {N} more ...`, so we don't want to use the typeNode and will use `type.types` instead which
        // is not shortened. In this case we want to do nothing and use the type-based union check
        if (t.isUnionType(typeNode) &&
            type.isUnion() &&
            typeNode.types.some(function (v) {
                return t.isIdentifier(v) ||
                    (t.isTypeReference(v) &&
                        t.isIdentifier(v.typeName) &&
                        v.typeName.escapedText.includes('more'));
            })) {
            exceptions = true;
        }
        // A function TypeNode looses a lot of information. We'll fallback to using the type. A Function
        // is an object with call signatures which is caught in the `t.isObject()` check
        if (t.isFunctionType(typeNode)) {
            exceptions = true;
        }
        // A TypeNode of `keyof` looses type information
        if (t.isTypeOperator(typeNode) && typeNode.operator === ts.SyntaxKind.KeyOfKeyword) {
            exceptions = true;
        }
        // A TypeNode of a Type literal looses symbol and JSDoc info
        if (t.isTypeLiteral(typeNode)) {
            exceptions = true;
        }
        // We want type unions to override type references that are not exported
        if (t.isTypeReference(typeNode) && type.isUnion()) {
            exceptions = true;
        }
        if (!exceptions) {
            try {
                return getValueFromNode(parser, typeNode);
            }
            catch (e) {
                // If we are here, we've run into an issue parsing the TypeNode. This could happen if a
                // Synthetic node contains `keyof` which is a type that doesn't point to a real AST node so
                // Typescript cannot evaluate the type and will result in an error. Under these cases, we'll
                // fall back to extracting info from the Type rather than the TypeNode.
                // We don't want to log this as it spams the console...
                // const message =
                //   e instanceof Error ? e.message : typeof e === 'string' ? e : 'Unknown Error';
                // console.info('Parsing of a TypeNode failed:', message);
            }
        }
    }
    /**
     * If the type is a union, we want to deal with it now to avoid issues with infinite loops and a
     * TypeNode.
     */
    if (type.isUnion()) {
        // If we got here, it means a TypeNode was a TypeReference that wasn't exported or a synthetic
        // TypeNode `keyof *` that `getValueFromNode` couldn't properly parse.
        var filteredTypes = type.types;
        return {
            kind: 'union',
            value: filteredTypes.map(function (t) { return getValueFromType(parser, t) || unknownValue(checker.typeToString(t)); }),
        };
    }
    if (type.isStringLiteral()) {
        return { kind: 'string', value: type.value };
    }
    if (type.isNumberLiteral()) {
        return { kind: 'number', value: type.value };
    }
    if (type.flags & ts.TypeFlags.String) {
        return { kind: 'primitive', value: 'string' };
    }
    if (type.flags & ts.TypeFlags.Number) {
        return { kind: 'primitive', value: 'number' };
    }
    if (type.flags & ts.TypeFlags.Null) {
        return { kind: 'primitive', value: 'null' };
    }
    if (type.flags & ts.TypeFlags.Undefined) {
        return { kind: 'primitive', value: 'undefined' };
    }
    if (type.flags & ts.TypeFlags.Boolean) {
        return { kind: 'primitive', value: 'boolean' };
    }
    if (type.flags & ts.TypeFlags.Any) {
        return { kind: 'primitive', value: 'any' };
    }
    if (type.flags & ts.TypeFlags.Void) {
        return { kind: 'primitive', value: 'void' };
    }
    if (type.flags & ts.TypeFlags.BooleanLiteral) {
        return { kind: 'boolean', value: checker.typeToString(type) === 'true' };
    }
    if (isObject(type)) {
        if (type.objectFlags & ts.ObjectFlags.ArrayLiteral) {
            var typeNode_1 = checker.typeToTypeNode(type, undefined, undefined);
            if (typeNode_1) {
                return getValueFromNode(parser, typeNode_1);
            }
        }
        return getObjectValueFromType(parser, type);
    }
    return;
}
export function unknownValue(nodeText) {
    return { kind: 'unknown', value: 'unknown', text: nodeText };
}
export function isExportedSymbol(checker, node) {
    var _a;
    var sourceFile = (_a = node.getSourceFile) === null || _a === void 0 ? void 0 : _a.call(node);
    if (sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.isDeclarationFile) {
        return true;
    }
    var symbol = getSymbolFromNode(checker, node);
    var declarationNode = getValueDeclaration(symbol);
    if (declarationNode) {
        t.getKindNameFromNode(declarationNode);
        // Check if the declaration's sourcefile is a declaration file
        if (declarationNode.getSourceFile().isDeclarationFile) {
            return true;
        }
        // consider imported symbols as externally available
        if (t.isImportClause(declarationNode) ||
            t.isImportDeclaration(declarationNode) ||
            t.isImportSpecifier(declarationNode)) {
            return true;
        }
        return isNodeExported(checker, declarationNode);
    }
    return false;
}
export function getValueDeclaration(symbol) {
    var _a;
    return (symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration) || ((_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) === null || _a === void 0 ? void 0 : _a[0]);
}
function isOptional(symbol) {
    return (symbol.getFlags() & ts.SymbolFlags.Optional) !== 0;
}
function safeGetText(checker, node) {
    var _a;
    if ((_a = node.getSourceFile) === null || _a === void 0 ? void 0 : _a.call(node)) {
        return node.getText();
    }
    // We have a synthetic TypeNode so we'll try to get useful text out of it.
    // It might be an identifier or literal type with a `text` property
    if (node.text) {
        return node.text;
    }
    // Try a symbol
    var symbol = getSymbolFromNode(checker, node);
    return (symbol === null || symbol === void 0 ? void 0 : symbol.name) || "SyntheticNode - " + t.getKindNameFromNode(node);
}
/**
 * Get export members of a node. Export members are additional members of a declaration.
 * For example:
 * ```ts
 * function myFn() {}
 *
 * myFn.foo = 'bar';
 * // `foo` is an export member of `myFn`
 * ```
 */
function getExportMembers(parser, node) {
    var _a;
    var checker = parser.checker;
    var exports = ((_a = getSymbolFromNode(checker, node)) === null || _a === void 0 ? void 0 : _a.exports) || new Map();
    var members = Array.from(exports.values())
        .map(function (symbol) {
        var declaration = getValueDeclaration(symbol);
        if (declaration) {
            return getValueFromNode(parser, declaration);
        }
        return;
    })
        .filter(function (i) { return !!i; });
    return members;
}
function getValueFromSignature(parser, declaration, signature) {
    var _a;
    var members = getExportMembers(parser, declaration);
    var typeParameters = (_a = signature.typeParameters) === null || _a === void 0 ? void 0 : _a.map(function (p) { return getValueFromType(parser, p); }).filter(function (v) { return (v === null || v === void 0 ? void 0 : v.kind) === 'typeParameter'; });
    var parameters = signature.parameters.map(function (s) {
        return getValueFromNode(parser, getValueDeclaration(s));
    });
    var returnType = declaration.type
        ? getValueFromNode(parser, declaration.type)
        : getValueFromType(parser, signature.getReturnType()) || unknownValue(declaration.getText());
    return {
        kind: 'function',
        typeParameters: typeParameters,
        parameters: parameters,
        members: members,
        returnType: returnType,
    };
}
function generateSignatureFromTypeNode(parser, node) {
    var _a;
    if (t.isFunctionType(node) || t.isMethodSignature(node)) {
        var parameters_1 = node.parameters.map(function (p) { return (getSymbolFromNode(parser.checker, p) || getSymbolFromNode(parser.checker, p.name)); });
        var typeParameters_1 = (_a = node.typeParameters) === null || _a === void 0 ? void 0 : _a.map(function (t) { return parser.checker.getTypeAtLocation(t); });
        return {
            parameters: parameters_1,
            declaration: node,
            typeParameters: typeParameters_1,
            getDeclaration: function () {
                return node;
            },
            getParameters: function () {
                return parameters_1;
            },
            getTypeParameters: function () {
                return typeParameters_1;
            },
            getReturnType: function () {
                return parser.checker.getTypeAtLocation(node.type || node);
            },
            getDocumentationComment: function () {
                var symbol = getSymbolFromNode(parser.checker, node);
                return symbol.getDocumentationComment(parser.checker);
            },
            getJsDocTags: function () {
                var symbol = getSymbolFromNode(parser.checker, node);
                return symbol.getJsDocTags();
            },
        };
    }
    return undefined;
}
export function getValueFromSignatureNode(parser, declaration) {
    var checker = parser.checker;
    var signature = checker.getSignatureFromDeclaration(declaration) ||
        generateSignatureFromTypeNode(parser, declaration);
    if (signature) {
        return getValueFromSignature(parser, declaration, signature);
    }
    return unknownValue(safeGetText(checker, declaration));
}
function getObjectValueFromType(parser, type) {
    var properties = type
        .getProperties()
        .map(function (symbol) {
        return getValueFromNode(parser, getValueDeclaration(symbol));
    })
        .filter(filterObjectProperties);
    var callSignatures = type.getCallSignatures().map(function (s) {
        return getValueFromSignatureNode(parser, s.getDeclaration());
    });
    if (callSignatures.length) {
        // We have a function and no properties
        if (properties) {
            callSignatures[0].members = properties;
        }
        return callSignatures[0];
    }
    return { kind: 'object', properties: properties };
}
/**
 * Get defaults from JSDoc tags if available and do some simple processing to extract useful type
 * information. JSDoc tags are not type checked, so our processing is limited.
 */
export function getDefaultFromTags(tags) {
    for (var _i = 0, tags_1 = tags; _i < tags_1.length; _i++) {
        var tag = tags_1[_i];
        if (tag.name === 'default') {
            var text = (tag.text || '').replace('{', '').replace('}', '');
            if ([
                'string',
                'number',
                'null',
                'undefined',
                'boolean',
                'any',
                'void',
                'unknown',
                'any',
            ].includes(text)) {
                return { kind: 'primitive', value: text };
            }
            if (['true', 'false'].includes(text)) {
                return { kind: 'boolean', value: text === 'true' };
            }
            if (!Number.isNaN(Number(text))) {
                return { kind: 'number', value: Number(text) };
            }
            if (/^['"][a-z0-9]+['"]$/.test(text)) {
                return { kind: 'string', value: text.replace(/["']/g, '') };
            }
            return { kind: 'symbol', name: text, value: text };
        }
    }
    return;
}
/**
 * This factory function makes it easy to create plugins by providing Typescript types
 *
 * @example
 * ```ts
 * import ts from 'typescript'
 *
 * const myPlugin = createParserPlugin((node, parser) => {
 *   // run tests on a node
 *  if (ts.isVariableDeclaration(node))
 * }
 * ```
 */
export function createParserPlugin(fn) {
    return fn;
}
/**
 * This small function makes it easier to write tests, but generally shouldn't be used. It creates a
 * new parser per file. Instead use the parser directly and call `parser.getExportedSymbols` for
 * each file to share memory.
 */
export function parse(program, fileName, plugins) {
    if (plugins === void 0) { plugins = []; }
    var parser = new DocParser(program, plugins);
    return parser.getExportedSymbols(fileName);
}
