import { commonjsRequire as V } from "../../../../../../_virtual/_commonjs-dynamic-modules.js";
import { d as W } from "../../../../../../_virtual/index8.js";
import "../../../../@babel_runtime@7.17.8/node_modules/@babel/runtime/helpers/interopRequireDefault.js";
import { __require as A } from "../../../../@babel_runtime@7.17.8/node_modules/@babel/runtime/helpers/extends.js";
import { __require as Y } from "../../../../@babel_runtime@7.17.8/node_modules/@babel/runtime/helpers/objectWithoutPropertiesLoose.js";
import z from "../../../../../../_virtual/___vite-browser-external.js";
import { __require as G } from "../../../../resolve@1.22.1/node_modules/resolve/index.js";
import { __require as H } from "../../../../cosmiconfig@6.0.0/node_modules/cosmiconfig/dist/index.js";
import { i as J } from "../../../../../../_virtual/interopRequireDefault.js";
(function(y) {
  var v = J.exports, _ = v(A()), $ = v(Y());
  const h = z, k = G(), x = /[./]macro(\.js)?$/, E = (e) => x.test(e);
  class q extends Error {
    constructor(r) {
      super(r), this.name = "MacroError", typeof Error.captureStackTrace == "function" ? Error.captureStackTrace(this, this.constructor) : this.stack || (this.stack = new Error(r).stack);
    }
  }
  let M = null;
  function R() {
    return M = M || H().cosmiconfigSync("babel-plugin-macros", {
      searchPlaces: ["package.json", ".babel-plugin-macrosrc", ".babel-plugin-macrosrc.json", ".babel-plugin-macrosrc.yaml", ".babel-plugin-macrosrc.yml", ".babel-plugin-macrosrc.js", "babel-plugin-macros.config.js"],
      packageProp: "babelMacros"
    });
  }
  function B(e, r = {}) {
    if (r.configName === "options")
      throw new Error('You cannot use the configName "options". It is reserved for babel-plugin-macros.');
    return o.isBabelMacro = !0, o.options = r, o;
    function o(t) {
      const {
        source: n,
        isBabelMacrosCall: i
      } = t;
      if (!i)
        throw new q(`The macro you imported from "${n}" is being executed outside the context of compilation with babel-plugin-macros. This indicates that you don't have the babel plugin "babel-plugin-macros" configured correctly. Please see the documentation for how to configure babel-plugin-macros properly: https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md`);
      return e(t);
    }
  }
  function C(e, r) {
    return k.sync(e, {
      basedir: r,
      paths: [h.resolve(__dirname, "../../")]
    });
  }
  function I(e, r = {}) {
    let {
      require: o = V,
      resolvePath: t = C,
      isMacrosName: n = E
    } = r, i = (0, $.default)(r, ["require", "resolvePath", "isMacrosName"]);
    function l(d) {
      const a = o(d);
      return a && a.__esModule && a.default ? a.default : a;
    }
    return {
      name: "macros",
      visitor: {
        Program(d, a) {
          d.traverse({
            ImportDeclaration(u) {
              if (!w(u, {
                node: {
                  source: {
                    value: (m) => n(m)
                  }
                }
              }))
                return;
              const c = u.node.specifiers.map((m) => ({
                localName: m.local.name,
                importedName: m.type === "ImportDefaultSpecifier" ? "default" : m.imported.name
              })), f = u.node.source.value, p = j({
                path: u,
                imports: c,
                source: f,
                state: a,
                babel: e,
                interopRequire: l,
                resolvePath: t,
                options: i
              });
              (!p || !p.keepImports) && u.remove();
            },
            VariableDeclaration(u) {
              const b = (c) => w(c, {
                node: {
                  init: {
                    callee: {
                      type: "Identifier",
                      name: "require"
                    },
                    arguments: (f) => f.length === 1 && n(f[0].value)
                  }
                }
              });
              u.get("declarations").filter(b).forEach((c) => {
                const f = c.node.id.name ? [{
                  localName: c.node.id.name,
                  importedName: "default"
                }] : c.node.id.properties.map((s) => ({
                  localName: s.value.name,
                  importedName: s.key.name
                })), p = c.get("init"), m = p.node.arguments[0].value, g = j({
                  path: p,
                  imports: f,
                  source: m,
                  state: a,
                  babel: e,
                  interopRequire: l,
                  resolvePath: t,
                  options: i
                });
                (!g || !g.keepImports) && c.remove();
              });
            }
          });
        }
      }
    };
  }
  function j({
    path: e,
    imports: r,
    source: o,
    state: t,
    babel: n,
    interopRequire: i,
    resolvePath: l,
    options: d
  }) {
    const {
      file: {
        opts: {
          filename: a = ""
        }
      }
    } = t;
    let u = !1;
    const b = r.reduce((s, {
      importedName: P,
      localName: L
    }) => {
      const S = e.scope.getBinding(L);
      return s[P] = S.referencePaths, u = u || Boolean(s[P].length), s;
    }, {}), c = o.indexOf(".") === 0, f = l(o, h.dirname(F(a))), p = i(f);
    if (!p.isBabelMacro)
      throw new Error(`The macro imported from "${o}" must be wrapped in "createMacro" which you can get from "babel-plugin-macros". Please refer to the documentation to see how to do this properly: https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/author.md#writing-a-macro`);
    const m = O(p, a, o, d);
    let g;
    try {
      t.file.scope.path.traverse({
        Identifier() {
        }
      }), g = p({
        references: b,
        source: o,
        state: t,
        babel: n,
        config: m,
        isBabelMacrosCall: !0
      });
    } catch (s) {
      throw s.name === "MacroError" || (s.message = `${o}: ${s.message}`, c || (s.message = `${s.message} Learn more: https://www.npmjs.com/package/${o.replace(
        /^((?:@[^/]+\/)?[^/]+).*/,
        "$1"
      )}`)), s;
    }
    return g;
  }
  function T(e, r) {
    try {
      const o = R().search(r);
      if (o)
        return {
          options: o.config[e],
          path: o.filepath
        };
    } catch (o) {
      return {
        error: o
      };
    }
    return {};
  }
  function D(e, r) {
    if (r.hasOwnProperty(e))
      if (r[e] && typeof r[e] != "object")
        console.error(`The macro plugin options' ${e} property was not an object or null.`);
      else
        return {
          options: r[e]
        };
    return {};
  }
  function O(e, r, o, t) {
    const {
      configName: n
    } = e.options;
    if (n) {
      const i = T(n, r), l = D(n, t);
      if (l.options === void 0 && i.options === void 0 && i.error !== void 0)
        throw console.error(`There was an error trying to load the config "${n}" for the macro imported from "${o}. Please see the error thrown for more information.`), i.error;
      if (i.options !== void 0 && l.options !== void 0 && typeof i.options != "object")
        throw new Error(`${i.path} specified a ${n} config of type ${typeof l.options}, but the the macros plugin's options.${n} did contain an object. Both configs must contain objects for their options to be mergeable.`);
      return (0, _.default)({}, l.options, {}, i.options);
    }
  }
  function F(e) {
    return h.isAbsolute(e) ? e : h.join(process.cwd(), e);
  }
  function w(e, r) {
    return e && r && Object.keys(r).every((o) => {
      const t = r[o], n = e[o];
      return typeof t == "function" ? t(n) : N(t) ? t === n : w(n, t);
    });
  }
  function N(e) {
    return e == null || /^[sbn]/.test(typeof e);
  }
  y.exports = I, Object.assign(y.exports, {
    createMacro: B,
    MacroError: q
  });
})(W);
//# sourceMappingURL=index.js.map
