"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _loaderUtils() {
  const data = require("loader-utils");

  _loaderUtils = function _loaderUtils() {
    return data;
  };

  return data;
}

function _core() {
  const data = require("@babel/core");

  _core = function _core() {
    return data;
  };

  return data;
}

function _core2() {
  const data = _interopRequireDefault(require("@svgr/core"));

  _core2 = function _core2() {
    return data;
  };

  return data;
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function svgrLoader(source) {
  const callback = this.async();

  const _ref = (0, _loaderUtils().getOptions)(this) || {},
        _ref$babel = _ref.babel,
        babel = _ref$babel === void 0 ? true : _ref$babel,
        options = _objectWithoutProperties(_ref, ["babel"]);

  const readSvg = () => {
    return new Promise((resolve, reject) => {
      this.fs.readFile(this.resourcePath, (err, result) => {
        if (err) reject(err);
        resolve(result);
      });
    });
  };

  const exportMatches = source.toString('utf-8').match(/^module.exports\s*=\s*(.*)/);
  const previousExport = exportMatches ? exportMatches[1] : null;

  const pBabelTransform = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator(function* (jsCode) {
      return new Promise((resolve, reject) => {
        (0, _core().transform)(jsCode, {
          babelrc: false,
          // Unless having this, babel will merge the config with global 'babel.config.js' which may causes some problems such as using react-hot-loader/babel in babel.config.js
          configFile: false,
          presets: [require.resolve('@babel/preset-react'), [require.resolve('@babel/preset-env'), {
            modules: false
          }]],
          plugins: [require.resolve('@babel/plugin-transform-react-constant-elements')]
        }, (err, result) => {
          if (err) reject(err);else resolve(result.code);
        });
      });
    });

    return function pBabelTransform(_x) {
      return _ref2.apply(this, arguments);
    };
  }();

  const tranformSvg = svg => {
    return (0, _core2().default)(svg, options, {
      webpack: {
        previousExport
      },
      filePath: this.resourcePath
    }).then(jsCode => babel ? pBabelTransform(jsCode) : jsCode).then(result => callback(null, result)).catch(err => callback(err));
  };

  if (exportMatches) {
    readSvg().then(tranformSvg);
  } else {
    tranformSvg(source);
  }
}

var _default = svgrLoader;
exports.default = _default;