"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 _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));

var _find = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/find"));

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 _globule = _interopRequireDefault(require("globule"));

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

var _default = () => (0, _plugin.default)('COPY I18N', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  try {
    return new _promise.default((resolve, reject) => {
      //copy the $file to $destinationDir
      const copyFile = (file, destinationDir) => {
        //gets file name and adds it to destinationFileDir
        const fileName = _path.default.basename(file);

        let dirName = _path.default.dirname(file);

        if (dirName) {
          dirName = dirName.split('/').pop();
        }

        const source = _fs.default.createReadStream(file);

        const dest = _fs.default.createWriteStream(_path.default.resolve(destinationDir, fileName));

        source.pipe(dest);
        source.on('end', () => {});
        source.on('error', err => {
          console.log(err);
        });
      };

      const copyLanguagePacks = (locales, workingDirectory, packageFolder, tmpDirectory) => {
        try {
          (0, _map.default)(locales).call(locales, locale => {
            const localeFolder = _path.default.join(workingDirectory, packageFolder, locale);

            const result = (0, _find.default)(_globule.default).call(_globule.default, {
              src: ['./src/apps/**/*.json', './src/packages/**/*.json'],
              cwd: localeFolder
            });
            const destinationDir = tmpDirectory + '/' + locale;

            if (!_fs.default.existsSync(destinationDir)) {
              _fs.default.mkdirSync(destinationDir);
            }

            (0, _map.default)(result).call(result, file => {
              copyFile(_path.default.resolve('../../packages/', locale, file), destinationDir);
            });
            console.log('Finished copying:', locale);
          });
        } catch (err) {
          console.error(err);
        }

        resolve();
      };

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

          for (const prop in packageJson.dependencies) {
            if ((0, _startsWith.default)(prop).call(prop, 'localization-')) {
              locales.push(prop);
            }
          }

          console.log('Number of localization packages found:', locales.length);

          if (locales.length > 0) {
            //Copy locales to /public/ folder
            const workingDirectory = process.cwd();

            const tmpDirectory = _path.default.join(workingDirectory, '/_i18n');

            const packageFolder = '../../packages';

            if (!_fs.default.exists(tmpDirectory)) {
              _fs.default.mkdir(tmpDirectory, /*#__PURE__*/function () {
                var _ref3 = (0, _asyncToGenerator2.default)(function* (err, results) {
                  copyLanguagePacks(locales, workingDirectory, packageFolder, tmpDirectory);
                });

                return function (_x3, _x4) {
                  return _ref3.apply(this, arguments);
                };
              }());
            } else {
              copyLanguagePacks(locales, workingDirectory, packageFolder, tmpDirectory);
            }
          } else {
            resolve();
          }
        });

        return function (_x, _x2) {
          return _ref2.apply(this, arguments);
        };
      }());
    });
  } catch (e) {
    return _promise.default.reject(e);
  }
}));

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