'use strict';

const kit = require('@nuxt/kit');
const fsExtra = require('fs-extra');
const sirv = require('sirv');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }

const sirv__default = /*#__PURE__*/_interopDefaultCompat(sirv);

function isObject(value) {
  return value !== null && typeof value === "object";
}
function _defu(baseObject, defaults, namespace = ".", merger) {
  if (!isObject(defaults)) {
    return _defu(baseObject, {}, namespace, merger);
  }
  const object = Object.assign({}, defaults);
  for (const key in baseObject) {
    if (key === "__proto__" || key === "constructor") {
      continue;
    }
    const value = baseObject[key];
    if (value === null || value === void 0) {
      continue;
    }
    if (merger && merger(object, key, value, namespace)) {
      continue;
    }
    if (Array.isArray(value) && Array.isArray(object[key])) {
      object[key] = [...value, ...object[key]];
    } else if (isObject(value) && isObject(object[key])) {
      object[key] = _defu(
        value,
        object[key],
        (namespace ? `${namespace}.` : "") + key.toString(),
        merger
      );
    } else {
      object[key] = value;
    }
  }
  return object;
}
function createDefu(merger) {
  return (...arguments_) => (
    // eslint-disable-next-line unicorn/no-array-reduce
    arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
  );
}
const defu = createDefu();

const components = [
  "EBody",
  "EButton",
  "EColumn",
  "EContainer",
  "EFont",
  "EHead",
  "EHeading",
  "EHr",
  "EHtml",
  "EImg",
  "ELink",
  "EPreview",
  "ERow",
  "ESection",
  "EText",
  "ETailwind",
  "EMarkdown"
];
const PATH = "/__vue_email__";
const PATH_PLAYGROUND = `${PATH}/client`;
const module$1 = kit.defineNuxtModule({
  meta: {
    name: "vue-email",
    configKey: "vueEmail",
    compatibility: {
      nuxt: "^3.0.0",
      bridge: false
    }
  },
  defaults(nuxt) {
    const isDev = process.env.NODE_ENV === "development" || nuxt.options.dev;
    return {
      baseUrl: null,
      playground: isDev
    };
  },
  setup(options, nuxt) {
    const { resolve } = kit.createResolver((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('nuxt.cjs', document.baseURI).href)));
    const playgroundDir = resolve("../dist/client");
    nuxt.options.runtimeConfig.public.vueEmailOptions = options;
    if (!nuxt.options.build.transpile)
      nuxt.options.build.transpile = [];
    const transpileList = ["defu", "vue-email"];
    transpileList.forEach((pkgName) => {
      if (!nuxt.options.build.transpile.includes(pkgName))
        nuxt.options.build.transpile.push(pkgName);
    });
    nuxt.hook("nitro:config", (nitroConfig) => {
      nitroConfig.alias = nitroConfig.alias || {};
      nitroConfig.externals = defu(typeof nitroConfig.externals === "object" ? nitroConfig.externals : {}, {
        inline: [resolve("./runtime")]
      });
      nitroConfig.alias["#vue-email"] = resolve("./runtime/server/services");
      nitroConfig.serverAssets = nitroConfig.serverAssets || [];
      nitroConfig.serverAssets.push({
        baseName: "emails",
        dir: "../emails"
      });
    });
    if (options.playground) {
      kit.addServerHandler({
        handler: resolve("./runtime/server/api/emails.get"),
        route: "/api/emails",
        method: "get",
        lazy: true
      });
      kit.addServerHandler({
        handler: resolve("./runtime/server/api/render/[file].get"),
        route: "/api/render/:file",
        method: "get",
        lazy: true
      });
      nuxt.hook("vite:serverCreated", async (server) => {
        if (await fsExtra.pathExists(playgroundDir))
          server.middlewares.use(PATH_PLAYGROUND, sirv__default(playgroundDir, { single: true, dev: true }));
      });
      nuxt.hook("devtools:customTabs", (iframeTabs) => {
        iframeTabs.push({
          name: "vueemail",
          title: "Vue Email",
          icon: "twemoji:incoming-envelope",
          view: {
            type: "iframe",
            src: PATH_PLAYGROUND
          }
        });
      });
    }
    kit.addTemplate({
      filename: "types/vue-email.d.ts",
      getContents: () => ["declare module '#vue-email' {", `  const useCompiler: typeof import('${resolve("./runtime/server/services")}').useCompiler`, "}"].join("\n")
    });
    nuxt.hook("prepare:types", (options2) => {
      options2.references.push({
        path: resolve(nuxt.options.buildDir, "types/vue-email.d.ts")
      });
    });
    kit.addPlugin(resolve("./runtime/templates/vue-email"));
    components.forEach((component) => {
      kit.addComponent({
        name: component,
        export: component,
        filePath: "vue-email"
      });
    });
    kit.addImportsSources({
      from: "vue-email",
      imports: ["useRender"]
    });
  }
});

module.exports = module$1;
