"use strict";

var _path = _interopRequireDefault(require("path"));

var _loaderUtils = _interopRequireDefault(require("loader-utils"));

var _common = require("../common");

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

module.exports = function (content) {
  let {
    userRequest
  } = this._module.issuer;
  this.cacheable && this.cacheable();

  if (!this.emitFile) {
    throw new Error('emitFile is required from module system');
  }

  let options = _loaderUtils.default.getOptions(this) || {};
  let query = this.resourceQuery ? _loaderUtils.default.parseQuery(this.resourceQuery) : {}; // let configKey = query.config || 'fileLoader';

  let config = {
    // publicPath: false,
    useRelativePath: false,
    name: '[hash].[ext]' // publicPathStrigify: true

  }; // options takes precedence over config

  Object.keys(options).forEach(attr => {
    config[attr] = options[attr];
  }); // query takes precedence over config and options

  Object.keys(query).forEach(attr => {
    config[attr] = query[attr];
  });
  let context = config.context || options.context;

  let url = _loaderUtils.default.interpolateName(this, config.name, {
    context: context,
    content: content,
    regExp: config.regExp
  });

  let outputPath = '';
  let filePath = this.resourcePath;

  if (config.useRelativePath) {
    let issuerContext = this._module && this._module.issuer && this._module.issuer.context || context;

    let relativeUrl = issuerContext && _path.default.relative(issuerContext, filePath).split(_path.default.sep).join('/');

    let relativePath = relativeUrl && `${_path.default.dirname(relativeUrl)}/`;

    if (~relativePath.indexOf('../')) {
      outputPath = _path.default.posix.join(outputPath, relativePath, url);
    } else {
      outputPath = relativePath + url;
    }

    url = relativePath + url;
  } else if (config.outputPath) {
    // support functions as outputPath to generate them dynamically
    outputPath = typeof config.outputPath === 'function' ? config.outputPath(url) : config.outputPath + url;
    url = outputPath;
  } else {
    outputPath = url;
  }

  let requestFromJS = /\.js$/.test(userRequest);
  let publicPath = requestFromJS ? (0, _common.getPublicPathConfig)(url) : `__webpack_public_path__ + ${JSON.stringify(url)}`; // if (config.publicPath !== false) {
  //   // support functions as publicPath to generate them dynamically
  //   if (config.publicPathStringify) {
  //     publicPath = JSON.Stringify(
  //       typeof config.publicPath === 'function'
  //         ? config.publicPath(url)
  //         : config.publicPath + url
  //     );
  //   } else {
  //     publicPath =
  //       typeof config.publicPath === 'function'
  //         ? config.publicPath(url)
  //         : config.publicPath + url;
  //   }
  // }

  if (query.emitFile === undefined || query.emitFile) {
    this.emitFile(outputPath, content);
  }

  return `module.exports = ${publicPath};`;
};

module.exports.raw = true;