"use strict";

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = void 0;

var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));

var _startsWith = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/starts-with"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator"));

var _fs = _interopRequireDefault(require("fs"));

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

var _child_process = require("child_process");

var _plugin = _interopRequireDefault(require("@start/plugin"));

var _default = theme => (0, _plugin.default)('MERGE THEME', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  if (!theme) {
    reporter.emit('message', 'No theme found, skipping step');
    return;
  } else {
    reporter.emit('message', 'Theme ' + theme + ' found');
  }

  const newTheme = 'theme-' + theme;

  try {
    return new _promise.default((resolve, reject) => {
      const writeLessFiles = () => {
        const appLess = '@import "~' + newTheme + '/styles/base.less";';

        _fs.default.writeFile('./app/assets/styles/App.less', appLess, 'utf-8', /*#__PURE__*/function () {
          var _ref2 = (0, _asyncToGenerator2.default)(function* (err, result) {
            if (!!err) reject(err);
            const fontLess = '@import "~' + newTheme + '/styles/fonts/fonts.less";';

            _fs.default.writeFile('./app/assets/styles/Fonts.less', fontLess, 'utf-8', /*#__PURE__*/function () {
              var _ref3 = (0, _asyncToGenerator2.default)(function* (err, result) {
                if (!!err) reject(err);
                return resolve();
              });

              return function (_x3, _x4) {
                return _ref3.apply(this, arguments);
              };
            }());
          });

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

      _fs.default.readFile('./package.json', 'utf-8', /*#__PURE__*/function () {
        var _ref4 = (0, _asyncToGenerator2.default)(function* (err, data) {
          if (!!err) reject(err);
          const packageJson = JSON.parse(data);
          let themeAlreadyInstalled = false;

          for (const prop in packageJson.dependencies) {
            if ((0, _startsWith.default)(prop).call(prop, newTheme)) {
              themeAlreadyInstalled = true;
            }
          }

          if (themeAlreadyInstalled) {
            reporter.emit('Theme is already installed');
            return writeLessFiles();
          } else {
            const workingDirectory = process.cwd();
            const packageFolder = '../../packages';

            const themeFolder = _path.default.join(workingDirectory, packageFolder, newTheme);

            const yarnCommand = `yarn add file:${themeFolder}`;
            (0, _child_process.exec)(yarnCommand, err => {
              if (!!err) reject(err);
              return writeLessFiles();
            });
          }
        });

        return function (_x5, _x6) {
          return _ref4.apply(this, arguments);
        };
      }());
    });
  } catch (e) {
    return _promise.default.reject(e);
  }
}));

exports.default = _default;
module.exports = exports.default;