var _bindInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/bind");

var _context;

const less = require('less');

const pify = require('pify');

const processResult = require('less-loader/dist/processResult');

const getOptions = require('less-loader/dist/getOptions');

const CosmicLessResolver = require('../../../lessPlugins/cosmic-less-resolver');

const getThemePath = require('../../../utils/cosmic-less-theme');

const render = pify(_bindInstanceProperty(_context = less.render).call(_context, less));
/**
 * Webpack loader that inherits less-loader but adds the CosmicLessResolver plugin
 * @param fileContent
 */

module.exports = function (source) {
  this.query.paths = this.query.paths || [];
  const options = getOptions(this);
  const done = this.async();
  const isSync = typeof done !== 'function';

  if (isSync) {
    throw new Error('Synchronous compilation is not supported anymore. See https://github.com/webpack-contrib/less-loader/issues/84');
  }

  options.plugins.push(new CosmicLessResolver({
    loaderContext: this,
    theme: getThemePath(this.query.theme),
    scope: this.query.scope,
    debug: this.query.debug
  }));
  processResult(this, render(source, options));
};