"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.shouldSkipAlias = shouldSkipAlias;
exports.isParameter = isParameter;
exports.getTextOfJsxAttributeName = getTextOfJsxAttributeName;
exports.getContainingObjectLiteralElement = getContainingObjectLiteralElement;
exports.getPropertySymbolFromContextualType = getPropertySymbolFromContextualType;
exports.getNameFromPropertyName = getNameFromPropertyName;
exports.isStringOrNumericLiteralLike = isStringOrNumericLiteralLike;
exports.getTextOfIdentifierOrLiteral = getTextOfIdentifierOrLiteral;
exports.getTextOfJsxNamespacedName = getTextOfJsxNamespacedName;
const ts = __importStar(require("typescript"));
// Functions in this file are based directly off corresponding functions
// in the TypeScript codebase.
function shouldSkipAlias(node) {
    switch (node.kind) {
        case ts.SyntaxKind.ImportClause:
        case ts.SyntaxKind.ImportEqualsDeclaration: {
            // TODO: How do we test this code path?
            return true;
        }
        case ts.SyntaxKind.ImportSpecifier: {
            return node.parent.kind === ts.SyntaxKind.NamedImports;
        }
        case ts.SyntaxKind.BindingElement:
        case ts.SyntaxKind.VariableDeclaration: {
            // TODO: How do we test this code path?
            return (isInJSFile(node) &&
                isVariableDeclarationInitializedToBareOrAccessedRequire(node));
        }
        default: {
            return false;
        }
    }
}
function isInJSFile(node) {
    return !!(node.flags & ts.NodeFlags.JavaScriptFile);
}
function isVariableDeclarationInitializedToBareOrAccessedRequire(node) {
    if (node.kind === ts.SyntaxKind.BindingElement) {
        node = node.parent.parent;
    }
    return isVariableDeclaration(node) && !!node.initializer;
    // FIXME: This requires inlining a bunch of more definitions.
    //       ts.isRequireCall(allowAccessedRequire ? ts.getLeftmostAccessExpression(node.initializer) : node.initializer, /*requireStringLiteralLikeArgument*/ true);
}
function isVariableDeclaration(node) {
    return node.kind === ts.SyntaxKind.VariableDeclaration;
}
function isParameter(sym) {
    var _a;
    // based on isFirstDeclarationOfSymbolParameter
    const declaration = (_a = sym.declarations) === null || _a === void 0 ? void 0 : _a[0];
    return !!ts.findAncestor(declaration, (node) => ts.isParameter(node)
        ? true
        : ts.isBindingElement(node) ||
            ts.isObjectBindingPattern(node) ||
            ts.isArrayBindingPattern(node)
            ? false
            : 'quit');
}
// The corresponding function is marked @internal here:
// https://sourcegraph.com/github.com/microsoft/TypeScript@fbcdb8cf4fbbbea0111a9adeb9d0d2983c088b7c/-/blob/src/compiler/utilities.ts?L10586-10589
function getTextOfJsxAttributeName(node) {
    return ts.isIdentifier(node)
        ? ts.idText(node)
        : `${ts.idText(node.namespace)}:${ts.idText(node.name)}`;
}
// https://sourcegraph.com/github.com/microsoft/TypeScript@2c23beae0297fe3e57868d02af8c9084b136f78c/-/blob/src/services/services.ts?L3534
function getContainingObjectLiteralElement(node) {
    const element = getContainingObjectLiteralElementWorker(node);
    return element &&
        (ts.isObjectLiteralExpression(element.parent) ||
            ts.isJsxAttributes(element.parent))
        ? element
        : undefined;
}
// https://sourcegraph.com/github.com/microsoft/TypeScript@2c23beae0297fe3e57868d02af8c9084b136f78c/-/blob/src/services/services.ts?L3538
function getContainingObjectLiteralElementWorker(node) {
    switch (node.kind) {
        case ts.SyntaxKind.StringLiteral:
        case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
        case ts.SyntaxKind.NumericLiteral: {
            if (node.parent.kind === ts.SyntaxKind.ComputedPropertyName) {
                return ts.isObjectLiteralElement(node.parent.parent)
                    ? node.parent.parent
                    : undefined;
            }
            // falls through
        }
        case ts.SyntaxKind.Identifier: {
            return ts.isObjectLiteralElement(node.parent) &&
                (node.parent.parent.kind === ts.SyntaxKind.ObjectLiteralExpression ||
                    node.parent.parent.kind === ts.SyntaxKind.JsxAttributes) &&
                node.parent.name === node
                ? node.parent
                : undefined;
        }
    }
    return undefined;
}
function getPropertySymbolFromContextualType(node, contextualType) {
    const name = getNameFromPropertyName(node.name);
    if (!name) {
        return undefined;
    }
    return contextualType.getProperty(name);
}
// https://sourcegraph.com/github.com/microsoft/TypeScript@2c23beae0297fe3e57868d02af8c9084b136f78c/-/blob/src/services/utilities.ts?L2433
function getNameFromPropertyName(name) {
    return name.kind === ts.SyntaxKind.ComputedPropertyName
        ? // treat computed property names where expression is string/numeric literal as just string/numeric literal
            isStringOrNumericLiteralLike(name.expression)
                ? name.expression.text
                : undefined
        : ts.isPrivateIdentifier(name)
            ? ts.idText(name)
            : getTextOfIdentifierOrLiteral(name);
}
// https://sourcegraph.com/github.com/microsoft/TypeScript@2c23beae0297fe3e57868d02af8c9084b136f78c/-/blob/src/compiler/utilities.ts?L5269
function isStringOrNumericLiteralLike(node) {
    return ts.isStringLiteralLike(node) || ts.isNumericLiteral(node);
}
// https://sourcegraph.com/github.com/microsoft/TypeScript@2c23beae0297fe3e57868d02af8c9084b136f78c/-/blob/src/compiler/utilities.ts?L5346
function getTextOfIdentifierOrLiteral(node) {
    return ts.isMemberName(node)
        ? ts.idText(node)
        : ts.isJsxNamespacedName(node)
            ? getTextOfJsxNamespacedName(node)
            : node.text;
}
// https://sourcegraph.com/github.com/microsoft/TypeScript@2c23beae0297fe3e57868d02af8c9084b136f78c/-/blob/src/compiler/utilities.ts?L11012
function getTextOfJsxNamespacedName(node) {
    return `${ts.idText(node.namespace)}:${ts.idText(node.name)}`;
}
