text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.transformNewExpression = exports.checkForLuaLibType = void 0;
  const lua = require("../../../LuaAST");
  const annotations_1 = require("../../utils/annotations");
  const diagnostics_1 = require("../../utils/diagnostics");
  const lualib_1 = require("../../utils/lualib");
  const call_1 = require("../call");
  const table_1 = require("../language-extensions/table");
  const builtinErrorTypeNames = new Set([
      "Error",
      "ErrorConstructor",
      "RangeError",
      "RangeErrorConstructor",
      "ReferenceError",
      "ReferenceErrorConstructor",
      "SyntaxError",
      "SyntaxErrorConstructor",
      "TypeError",
      "TypeErrorConstructor",
      "URIError",
      "URIErrorConstructor",
  ]);
  // TODO: Do it in identifier?
  function checkForLuaLibType(context, type) {
      if (!type.symbol)
          return;
      const name = context.checker.getFullyQualifiedName(type.symbol);
      switch (name) {
          case "Map":
              lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.Map);
              return;
          case "Set":
              lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.Set);
              return;
          case "WeakMap":
              lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.WeakMap);
              return;
          case "WeakSet":
              lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.WeakSet);
              return;
      }
      if (builtinErrorTypeNames.has(name)) {
          lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.Error);
      }
  }
  exports.checkForLuaLibType = checkForLuaLibType;
  const transformNewExpression = (node, context) => {
      var _a;
      const type = context.checker.getTypeAtLocation(node);
      const annotations = annotations_1.getTypeAnnotations(type);
      if (annotations.has(annotations_1.AnnotationKind.LuaTable)) {
          context.diagnostics.push(diagnostics_1.annotationRemoved(node, annotations_1.AnnotationKind.LuaTable));
      }
      if (table_1.isTableNewCall(context, node)) {
          return lua.createTableExpression(undefined, node);
      }
      const signature = context.checker.getResolvedSignature(node);
      const params = node.arguments
          ? call_1.transformArguments(context, node.arguments, signature)
          : [lua.createBooleanLiteral(true)];
      checkForLuaLibType(context, type);
      const name = context.transformExpression(node.expression);
      const customConstructorAnnotation = annotations.get(annotations_1.AnnotationKind.CustomConstructor);
      if (customConstructorAnnotation) {
          if (customConstructorAnnotation.args.length === 1) {
              return lua.createCallExpression(lua.createIdentifier(customConstructorAnnotation.args[0]), call_1.transformArguments(context, (_a = node.arguments) !== null && _a !== void 0 ? _a : []), node);
          }
          else {
              context.diagnostics.push(diagnostics_1.annotationInvalidArgumentCount(node, annotations_1.AnnotationKind.CustomConstructor, customConstructorAnnotation.args.length, 1));
          }
      }
      return lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.New, node, name, ...params);
  };
  exports.transformNewExpression = transformNewExpression;
  //# sourceMappingURL=new.js.map