"use strict";

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

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SDKApp = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactDndHtml5Backend = _interopRequireDefault(require("react-dnd-html5-backend"));

var _reactDnd = require("react-dnd");

var _lib = _interopRequireDefault(require("@instructure/ui-themeable/lib"));

var _ApplyLocale = _interopRequireDefault(require("@instructure/ui-i18n/lib/components/ApplyLocale"));

var _reactRedux = require("../../react-redux");

var _reduxStore = _interopRequireDefault(require("../../../reduxStore"));

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

var _locale = _interopRequireDefault(require("@instructure/quiz-common/lib/locale"));

var _features = require("../../api/features");

var _ui = require("../../actions/ui");

var _config = require("../../actions/config");

var _outcomes = require("../../actions/outcomes");

var _features2 = require("../../actions/features");

var _renderAlerts = _interopRequireDefault(require("../../util/renderAlerts"));

var _theme = _interopRequireDefault(require("./theme"));

var _CustomPropTypes = _interopRequireDefault(require("../../../common/util/CustomPropTypes"));

var _ConfirmationModal = _interopRequireDefault(require("../ConfirmationModal"));

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'fzweS',
  template: function template(theme) {
    return "\n\n.fzweS_dXHX{height:100%}";
  },
  'sdk': 'fzweS_dXHX'
};

var _ref =
/*#__PURE__*/
_react.default.createElement(_ConfirmationModal.default, null);

var SDKApp = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(SDKApp, _Component);

  function SDKApp(props) {
    var _this;

    (0, _classCallCheck2.default)(this, SDKApp);
    _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(SDKApp).call(this, props)); // TODO: move to something like this down the line
    // so we dont implicitly share state
    // this.store = this.configureStore()

    _this.store = _reduxStore.default;

    _this.setLocale();

    return _this;
  }

  (0, _createClass2.default)(SDKApp, [{
    key: "setLocale",
    value: function setLocale() {
      var locale = (0, _locale.default)(this.props, this.context); // TODO: Move away from using built-in format-message formats so we can kill this setup call

      _formatMessage.default.setup({
        missingTranslation: 'ignore',
        locale: locale
      });
    }
  }, {
    key: "setAppContainer",
    value: function setAppContainer(appContainer) {
      var selectors = Array.isArray(appContainer) ? appContainer : [appContainer];
      return (0, _ui.set)('appContainer', selectors);
    }
  }, {
    key: "componentWillMount",
    value: function componentWillMount() {
      this.store.dispatch([(0, _config.setAPIEndpoint)(this.props.apiEndpoint), this.setAppContainer(this.props.appContainer), (0, _config.setUserToken)(this.props.userToken), (0, _outcomes.setOutcomesEndpoint)(this.props.outcomesEndpoint), (0, _outcomes.setOutcomesToken)(this.props.outcomesToken), (0, _outcomes.setContextUuid)(this.props.contextUuid)]);
      this.store.dispatch((0, _features.getFeatures)());

      if (this.props.itemBankSharingEnabled) {
        this.store.dispatch((0, _features2.addFeatures)('item_bank_sharing'));
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      // inserts the a11y friendly alerts
      // react app hooked up to the store
      (0, _renderAlerts.default)(this.store);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(newProps) {
      if (newProps.apiEndpoint !== this.props.apiEndpoint) {
        this.store.dispatch((0, _config.setAPIEndpoint)(newProps.apiEndpoint));
      }

      if (newProps.appContainer !== this.props.appContainer) {
        this.store.dispatch(this.setAppContainer(newProps.appContainer));
      }

      if (newProps.userToken !== this.props.userToken) {
        this.store.dispatch((0, _config.setUserToken)(newProps.userToken));
      }

      if (newProps.outcomesEndpoint !== this.props.outcomesEndpoint) {
        this.store.dispatch((0, _outcomes.setOutcomesEndpoint)(newProps.outcomesEndpoint));
      }

      if (newProps.outcomesToken !== this.props.outcomesToken) {
        this.store.dispatch((0, _outcomes.setOutcomesToken)(newProps.outcomesToken));
      }

      if (newProps.contextUuid !== this.props.contextUuid) {
        this.store.dispatch((0, _outcomes.setContextUuid)(newProps.contextUuid));
      }

      if (newProps.itemBankSharingEnabled !== this.props.itemBankSharingEnabled) {
        var toggleFeature = newProps.itemBankSharingEnabled ? _features2.addFeatures : _features2.removeFeatures;
        this.store.dispatch(toggleFeature('item_bank_sharing'));
      }
    }
  }, {
    key: "render",
    value: function render() {
      var store = this.store,
          props = this.props,
          context = this.context;
      return _react.default.createElement(_reactRedux.Provider, {
        store: store
      }, _react.default.createElement(_ApplyLocale.default, {
        locale: (0, _locale.default)(props, context),
        timezone: props.timeZone
      }, _react.default.createElement("div", {
        className: styles.sdk,
        "data-automation": "sdk-app"
      }, _ref, props.children)));
    }
  }]);
  SDKApp.displayName = "SDKApp";
  return SDKApp;
}(_react.Component), _class2.propTypes = {
  apiEndpoint: _propTypes.default.string.isRequired,
  appContainer: _CustomPropTypes.default.selectors.isRequired,
  children: _propTypes.default.node.isRequired,
  contextUuid: _propTypes.default.string,
  itemBankSharingEnabled: _propTypes.default.bool,
  locale: _propTypes.default.string,
  outcomesEndpoint: _propTypes.default.string,
  outcomesToken: _propTypes.default.string,
  timeZone: _propTypes.default.string,
  userToken: _propTypes.default.string.isRequired
}, _class2.contextTypes = {
  locale: _propTypes.default.string
}, _class2.defaultProps = {
  contextUuid: null,
  itemBankSharingEnabled: false,
  locale: null,
  outcomesEndpoint: null,
  outcomesToken: null,
  timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
}, _temp)) || _class);
exports.SDKApp = SDKApp;
var DragDroppableSDKApp = (0, _reactDnd.DragDropContext)(_reactDndHtml5Backend.default)(SDKApp);
var _default = DragDroppableSDKApp;
exports.default = _default;