'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _pathExtra = require('path-extra');

var _pathExtra2 = _interopRequireDefault(_pathExtra);

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _electron = require('electron');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _reactFontawesome = require('react-fontawesome');

var _reactFontawesome2 = _interopRequireDefault(_reactFontawesome);

var _reactBootstrap = require('react-bootstrap');

var ReactBootstrap = _interopRequireWildcard(_reactBootstrap);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

require('coffee-react/register');
require('babel-register')(require('../babel.config'));

const { Radio, Checkbox, FormControl } = ReactBootstrap;

// Environments
window.remote = _electron.remote;
window.ROOT = _pathExtra2.default.join(__dirname, '..');
window.EXROOT = _electron.remote.getGlobal('EXROOT');
window.APPDATA_PATH = _electron.remote.getGlobal('APPDATA_PATH');
window.PLUGIN_PATH = _pathExtra2.default.join(window.APPDATA_PATH, 'plugins');
window.POI_VERSION = _electron.remote.getGlobal('POI_VERSION');
window.SERVER_HOSTNAME = _electron.remote.getGlobal('SERVER_HOSTNAME');
window.MODULE_PATH = _electron.remote.getGlobal('MODULE_PATH');
window.appIcon = _electron.remote.getGlobal('appIcon');
window.isSafeMode = _electron.remote.getGlobal('isSafeMode');

if (window.isMain) {
  // Plugins
  _fsExtra2.default.ensureDirSync(window.PLUGIN_PATH);
  _fsExtra2.default.ensureDirSync(_pathExtra2.default.join(window.PLUGIN_PATH, 'node_modules')
  // Debug
  );window.dbg = require(_pathExtra2.default.join(window.ROOT, 'lib', 'debug'));
  window.dbg.init();
}

// Add ROOT to `require` search path
require('module').globalPaths.push(window.ROOT

// Shortcuts and Components
);window._ = _lodash2.default; // TODO: Backward compatibility
window.$ = param => document.querySelector(param);
window.$$ = param => document.querySelectorAll(param);
window.React = _react2.default;
window.ReactDOM = _reactDom2.default;
window.FontAwesome = _reactFontawesome2.default;
window.ReactBootstrap = ReactBootstrap;
// Workaround
window.ReactBootstrap.Input = class InputWorkAround extends _react2.default.Component {
  render() {
    switch (this.props.type) {
      case 'radio':
        {
          return _react2.default.createElement(
            Radio,
            this.props,
            this.props.label
          );
        }
      case 'checkbox':
        {
          return _react2.default.createElement(
            Checkbox,
            this.props,
            this.props.label
          );
        }
      case 'select':
        {
          return _react2.default.createElement(
            FormControl,
            _extends({ componentClass: 'select' }, this.props),
            this.props.children
          );
        }
      default:
        {
          return _react2.default.createElement(
            FormControl,
            this.props,
            this.props.children
          );
        }
    }
  }
};

// Polyfills
Object.clone = obj => JSON.parse(JSON.stringify(obj));
Object.remoteClone = obj => JSON.parse(window.remote.require('./lib/utils').remoteStringify(obj)

// Utils
);require('./env-parts/utils'

// Node modules
);const originConfig = _electron.remote.require('./lib/config');
window.ipc = _electron.remote.require('./lib/ipc');
window.proxy = _electron.remote.require('./lib/proxy');
window.CONST = Object.remoteClone(_electron.remote.require('./lib/constant'));
window.config = {};
for (const key in originConfig) {
  window.config[key] = originConfig[key];
}

// i18n config
require('./env-parts/i18n-config'

// window.notify
// msg=null: Sound-only notification.
);require('./env-parts/notif-center');
require('./env-parts/modal'

// Custom theme
// You should call window.applyTheme() to apply a theme properly.
);require('./env-parts/theme'

// Global data resolver
);require('./env-parts/data-resolver'

// Getter
);require('./env-parts/getter'

// Only used by main window
);if (window.isMain) {
  // Toast
  require('./env-parts/toast'
  // Add devtool debug message print
  );require('./env-parts/devtool-message');
}