var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var ast_exports = {};
__export(ast_exports, {
  extractMailComponents: () => extractMailComponents
});
module.exports = __toCommonJS(ast_exports);
var import_ends_with = __toESM(require("@babel/runtime-corejs3/core-js/instance/ends-with"));
var import_for_each = __toESM(require("@babel/runtime-corejs3/core-js/instance/for-each"));
var import_stringify = __toESM(require("@babel/runtime-corejs3/core-js/json/stringify"));
var import_node_fs = __toESM(require("node:fs"));
var import_parser = require("@babel/parser");
var import_traverse = __toESM(require("@babel/traverse"));
var t = __toESM(require("@babel/types"));
function extractMailComponents(filepath) {
  const plugins = [];
  if ((0, import_ends_with.default)(filepath).call(filepath, ".tsx")) {
    plugins.push("typescript");
    plugins.push("jsx");
  } else if ((0, import_ends_with.default)(filepath).call(filepath, ".jsx")) {
    plugins.push("jsx");
  }
  const templateAST = (0, import_parser.parse)(import_node_fs.default.readFileSync(filepath, "utf8"), {
    plugins,
    sourceType: "unambiguous"
  });
  const components = [];
  (0, import_traverse.default)(templateAST, {
    ExportNamedDeclaration(path) {
      const name = extractExportNamedDeclarationName(path);
      const props = extractExportNamedDeclarationProps(path);
      components.push({
        name,
        props
      });
    }
    // // @ts-expect-error TODO: Fix this type issue
    // if (t.isFunctionDeclaration(path.node.declaration)) {
    //   // extract the name
    //   const name = path.node.declaration.id.name
    //   // extract the props
    //   let props: string | null = null
    //   if (path.node.declaration.params.length > 0) {
    //     // We know we only care about the first param - the component props
    //     const propsParam = path.node.declaration.params[0]
    //     if (t.isIdentifier(propsParam)) {
    //       props = propsParam.name
    //     } else if (t.isObjectPattern(propsParam)) {
    //       // We want a { param: ?, param2: ? } style string
    //       const propsForJson = {}
    //       propsParam.properties.forEach((prop) => {
    //         if (t.isObjectProperty(prop) && t.isIdentifier(prop.key)) {
    //           propsForJson[prop.key.name] = '?'
    //         }
    //       })
    //       props = JSON.stringify(propsForJson, undefined, 2)
    //     } else if (
    //       t.isAssignmentPattern(propsParam) &&
    //       t.isObjectPattern(propsParam.left)
    //     ) {
    //       const propsForJson = {}
    //       propsParam.left.properties.forEach((prop) => {
    //         if (t.isObjectProperty(prop) && t.isIdentifier(prop.key)) {
    //           propsForJson[prop.key.name] = '?'
    //         }
    //       })
    //       props = JSON.stringify(propsForJson, undefined, 2)
    //     }
    //   }
    //   components.push({ name, props })
    // }
  });
  return components;
}
function extractExportNamedDeclarationName(path) {
  const declaration = path.node.declaration;
  if (t.isFunctionDeclaration(declaration)) {
    return declaration.id.name;
  } else if (t.isVariableDeclaration(declaration) && t.isIdentifier(declaration.declarations[0].id)) {
    return declaration.declarations[0].id.name;
  }
  console.log({
    declaration
  });
  return "Unknown";
}
function extractExportNamedDeclarationProps(path) {
  const declaration = path.node.declaration;
  if (t.isFunctionDeclaration(declaration)) {
    const param = declaration.params[0];
    if (t.isAssignmentPattern(param)) {
      if (t.isObjectPattern(param.left)) {
        var _context;
        const propsForJson = {};
        (0, import_for_each.default)(_context = param.left.properties).call(_context, (prop) => {
          if (t.isObjectProperty(prop) && t.isIdentifier(prop.key)) {
            propsForJson[prop.key.name] = "?";
          }
        });
        return (0, import_stringify.default)(propsForJson, void 0, 2);
      }
    } else if (t.isObjectPattern(param)) {
      var _context2;
      const propsForJson = {};
      (0, import_for_each.default)(_context2 = param.properties).call(_context2, (prop) => {
        if (t.isObjectProperty(prop) && t.isIdentifier(prop.key)) {
          propsForJson[prop.key.name] = "?";
        }
      });
      return (0, import_stringify.default)(propsForJson, void 0, 2);
    } else if (t.isIdentifier(param)) {
      return (0, import_stringify.default)(param.name, void 0, 2);
    }
  } else if (t.isVariableDeclaration(declaration)) {
    const init = declaration.declarations[0].init;
    if (t.isArrowFunctionExpression(init)) {
      const param = init.params[0];
      if (t.isIdentifier(param)) {
        return (0, import_stringify.default)(param.name, void 0, 2);
      } else if (t.isObjectPattern(param)) {
        var _context3;
        const propsForJson = {};
        (0, import_for_each.default)(_context3 = param.properties).call(_context3, (prop) => {
          if (t.isObjectProperty(prop) && t.isIdentifier(prop.key)) {
            propsForJson[prop.key.name] = "?";
          }
        });
        return (0, import_stringify.default)(propsForJson, void 0, 2);
      }
    }
  }
  return "";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  extractMailComponents
});
//# sourceMappingURL=ast.js.map
