text: "use strict";
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
      if (k2 === undefined) k2 = k;
      Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  }) : (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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
      __setModuleDefault(result, mod);
      return result;
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.sveltePreprocess = exports.transform = void 0;
  const utils_1 = require("./modules/utils");
  const tagInfo_1 = require("./modules/tagInfo");
  const language_1 = require("./modules/language");
  const prepareContent_1 = require("./modules/prepareContent");
  const markup_1 = require("./modules/markup");
  exports.transform = async (name, options, { content, map, filename, attributes }) => {
      if (options === false) {
          return { code: content };
      }
      if (typeof options === 'function') {
          return options({ content, map, filename, attributes });
      }
      // todo: maybe add a try-catch here looking for module-not-found errors
      const { transformer } = await Promise.resolve().then(() => __importStar(require(`./transformers/${name}`)));
      return transformer({
          content,
          filename,
          map,
          attributes,
          options: typeof options === 'boolean' ? null : options,
      });
  };
  function sveltePreprocess(_a) {
      var _b, _c;
      var { aliases, markupTagName = 'template', preserve = [], defaults, sourceMap = (_c = ((_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b.NODE_ENV) === 'development') !== null && _c !== void 0 ? _c : false, ...rest } = _a === void 0 ? {} : _a;
      const defaultLanguages = Object.freeze({
          markup: 'html',
          style: 'css',
          script: 'javascript',
          ...defaults,
      });
      const transformers = rest;
      if (aliases === null || aliases === void 0 ? void 0 : aliases.length) {
          language_1.addLanguageAlias(aliases);
      }
      const getTransformerOptions = (name, alias) => {
          const { [name]: nameOpts, [alias]: aliasOpts } = transformers;
          if (typeof aliasOpts === 'function')
              return aliasOpts;
          if (typeof nameOpts === 'function')
              return nameOpts;
          if (aliasOpts === false || nameOpts === false)
              return false;
          const opts = {};
          if (typeof nameOpts === 'object') {
              Object.assign(opts, nameOpts);
          }
          Object.assign(opts, language_1.getLanguageDefaults(name), language_1.getLanguageDefaults(alias));
          if (name !== alias && typeof aliasOpts === 'object') {
              Object.assign(opts, aliasOpts);
          }
          if (sourceMap && name in language_1.SOURCE_MAP_PROP_MAP) {
              const [propName, value] = language_1.SOURCE_MAP_PROP_MAP[name];
              opts[propName] = value;
          }
          return opts;
      };
      const getTransformerTo = (type, targetLanguage) => async (svelteFile) => {
          let { content, filename, lang, alias, dependencies, attributes, } = await tagInfo_1.getTagInfo(svelteFile);
          if (lang == null || alias == null) {
              alias = defaultLanguages[type];
              lang = language_1.getLanguageFromAlias(alias);
          }
          if (preserve.includes(lang) || preserve.includes(alias)) {
              return { code: content };
          }
          const transformerOptions = getTransformerOptions(lang, alias);
          content = prepareContent_1.prepareContent({
              options: transformerOptions,
              content,
          });
          if (lang === targetLanguage) {
              return { code: content, dependencies };
          }
          const transformed = await exports.transform(lang, transformerOptions, {
              content,
              filename,
              attributes,
          });
          return {
              ...transformed,
              dependencies: utils_1.concat(dependencies, transformed.dependencies),
          };
      };
      const scriptTransformer = getTransformerTo('script', 'javascript');
      const cssTransformer = getTransformerTo('style', 'css');
      const markupTransformer = getTransformerTo('markup', 'html');
      const markup = async ({ content, filename }) => {
          if (transformers.replace) {
              const transformed = await exports.transform('replace', transformers.replace, {
                  content,
                  filename,
              });
              content = transformed.code;
          }
          return markup_1.transformMarkup({ content, filename }, markupTransformer, {
              // we only pass the markupTagName because the rest of options
              // is fetched internally by the `markupTransformer`
              markupTagName,
          });
      };
      const script = async ({ content, attributes, filename, }) => {
          const transformResult = await scriptTransformer({
              content,
              attributes,
              filename,
          });
          let { code, map, dependencies, diagnostics } = transformResult;
          if (transformers.babel) {
              const transformed = await exports.transform('babel', getTransformerOptions('babel'), {
                  content: code,
                  map,
                  filename,
                  attributes,
              });
              code = transformed.code;
              map = transformed.map;
              dependencies = utils_1.concat(dependencies, transformed.dependencies);
              diagnostics = utils_1.concat(diagnostics, transformed.diagnostics);
          }
          return { code, map, dependencies, diagnostics };
      };
      const style = async ({ content, attributes, filename, }) => {
          const transformResult = await cssTransformer({
              content,
              attributes,
              filename,
          });
          let { code, map, dependencies } = transformResult;
          // istanbul ignore else
          if (await utils_1.hasDepInstalled('postcss')) {
              if (transformers.postcss) {
                  const { alias } = language_1.getLanguage(attributes);
                  const transformed = await exports.transform('postcss', getTransformerOptions('postcss', alias), { content: code, map, filename, attributes });
                  code = transformed.code;
                  map = transformed.map;
                  dependencies = utils_1.concat(dependencies, transformed.dependencies);
              }
              const transformed = await exports.transform('globalStyle', getTransformerOptions('globalStyle'), { content: code, map, filename, attributes });
              code = transformed.code;
              map = transformed.map;
          }
          else if ('global' in attributes) {
              console.warn(`[svelte-preprocess] 'global' attribute found, but 'postcss' is not installed.`);
          }
          return { code, map, dependencies };
      };
      return {
          defaultLanguages,
          markup,
          script,
          style,
      };
  }
  exports.sveltePreprocess = sveltePreprocess;
  