"use strict";

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

var _kind = _interopRequireDefault(require("@enact/core/kind"));

var _hoc = _interopRequireDefault(require("@enact/core/hoc"));

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

var _Pure = _interopRequireDefault(require("@enact/ui/internal/Pure"));

var _compose = _interopRequireDefault(require("ramda/src/compose"));

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

var _Pause = _interopRequireDefault(require("@enact/spotlight/Pause"));

var _Spinner = _interopRequireDefault(require("@enact/ui/Spinner"));

var _spotlight = _interopRequireDefault(require("@enact/spotlight"));

var _$L = _interopRequireDefault(require("../internal/$L"));

var _Marquee = _interopRequireDefault(require("../Marquee"));

var _Skinnable = _interopRequireDefault(require("../Skinnable"));

var _Spinner2 = _interopRequireDefault(require("./Spinner.less"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

/**
 * A component that shows spinning balls, with optional text as children.
 *
 * @class SpinnerCore
 * @memberof moonstone/Spinner
 * @ui
 * @private
 */
var SpinnerCore = (0, _kind.default)({
  name: 'SpinnerCore',
  propTypes: {
    css: _propTypes.default.object
  },
  styles: {
    css: _Spinner2.default
  },
  computed: {
    'aria-label': function ariaLabel(_ref) {
      var aria = _ref['aria-label'],
          children = _ref.children;

      if (aria) {
        return aria;
      } else if (!children) {
        return (0, _$L.default)('Loading');
      }
    }
  },
  render: function render(_ref2) {
    var children = _ref2.children,
        css = _ref2.css,
        rest = _objectWithoutProperties(_ref2, ["children", "css"]);

    return _react.default.createElement("div", _extends({
      "aria-live": "off",
      role: "alert"
    }, rest), _react.default.createElement("div", {
      className: css.ballDecorator
    }, _react.default.createElement("div", {
      className: "".concat(css.ball, " ").concat(css.ball1)
    }), _react.default.createElement("div", {
      className: "".concat(css.ball, " ").concat(css.ball2)
    }), _react.default.createElement("div", {
      className: "".concat(css.ball, " ").concat(css.ball3)
    })), children ? _react.default.createElement(_Marquee.default, {
      className: css.client,
      marqueeOn: "render",
      alignment: "center"
    }, children) : null);
  }
});
/**
 * The base component, defining all of the properties.
 *
 * @class SpinnerBase
 * @memberof moonstone/Spinner
 * @ui
 * @public
 */

var SpinnerBase = (0, _kind.default)({
  name: 'Spinner',
  propTypes:
  /** @lends moonstone/Spinner.SpinnerBase.prototype */
  {
    /**
     * Customizes the component by mapping the supplied collection of CSS class names to the
     * corresponding internal Elements and states of this component.
     *
     * The following classes are supported:
     *
     * * `spinner` - The root component class, unless there is a scrim. The scrim and floating layer can be a sibbling or parent to this root "spinner" element.
     *
     * @type {Object}
     * @public
     */
    css: _propTypes.default.object,

    /**
     * Removes the background color (making it transparent).
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    transparent: _propTypes.default.bool
  },
  defaultProps: {
    transparent: false
  },
  styles: {
    css: _Spinner2.default,
    publicClassNames: 'spinner'
  },
  computed: {
    className: function className(_ref3) {
      var children = _ref3.children,
          transparent = _ref3.transparent,
          styler = _ref3.styler;
      return styler.append({
        content: !!children,
        transparent: transparent
      });
    }
  },
  render: function render(_ref4) {
    var children = _ref4.children,
        css = _ref4.css,
        rest = _objectWithoutProperties(_ref4, ["children", "css"]);

    delete rest.transparent;
    return _react.default.createElement(_Spinner.default, _extends({}, rest, {
      css: css,
      component: SpinnerCore
    }), children);
  }
});
/**
 * A higher-order component that pauses spotlight when `blockClickOn` prop is `'screen'`.
 *
 * Resumes spotlight when unmounted. However, spotlight is not paused when `blockClickOn` prop is
 * `'container'`. Blocking spotlight within the container is up to app implementation.
 *
 * @hoc
 * @memberof moonstone/Spinner
 * @ui
 * @private
 */

exports.SpinnerBase = SpinnerBase;
var SpinnerSpotlightDecorator = (0, _hoc.default)(function (config, Wrapped) {
  var _class, _temp;

  // eslint-disable-line no-unused-vars
  return _temp = _class =
  /*#__PURE__*/
  function (_React$Component) {
    _inherits(_class, _React$Component);

    function _class() {
      var _this;

      _classCallCheck(this, _class);

      _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this));
      _this.paused = new _Pause.default('Spinner');
      return _this;
    }

    _createClass(_class, [{
      key: "componentWillMount",
      value: function componentWillMount() {
        var blockClickOn = this.props.blockClickOn;

        var current = _spotlight.default.getCurrent();

        if (blockClickOn === 'screen') {
          this.paused.pause();

          if (current) {
            current.blur();
          }
        }
      }
    }, {
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        var blockClickOn = this.props.blockClickOn;

        if (blockClickOn === 'screen') {
          _spotlight.default.focus();

          this.paused.resume();
        }
      }
    }, {
      key: "render",
      value: function render() {
        return _react.default.createElement(Wrapped, this.props);
      }
    }]);

    return _class;
  }(_react.default.Component), Object.defineProperty(_class, "displayName", {
    configurable: true,
    enumerable: true,
    writable: true,
    value: 'SpinnerSpotlightDecorator'
  }), Object.defineProperty(_class, "propTypes", {
    configurable: true,
    enumerable: true,
    writable: true,
    value:
    /** @lends moonstone/Spinner.Spinner.prototype */
    {
      /**
       * Determines how far the click-blocking should extend.
       *
       * It can be either `'screen'`, `'container'`, or `null`. `'screen'` pauses spotlight.
       * Changing this property to `'screen'` after creation is not supported.
       *
       * @type {String}
       * @default null
       * @public
       */
      blockClickOn: _propTypes.default.oneOf(['screen', 'container', null])
    }
  }), _temp;
});
/**
 * Moonstone-specific Spinner behaviors to apply to [Spinner]{@link moonstone/Spinner.Spinner}.
 *
 * @hoc
 * @memberof moonstone/Spinner
 * @mixes moonstone/Spinner.SpinnerSpotlightDecorator
 * @mixes moonstone/Skinnable.Skinnable
 * @public
 */

var SpinnerDecorator = (0, _compose.default)(_Pure.default, SpinnerSpotlightDecorator, _Skinnable.default);
/**
 * A Moonstone-styled Spinner.
 *
 * @class Spinner
 * @memberof moonstone/Spinner
 * @mixes moonstone/Spinner.SpinnerDecorator
 * @ui
 * @public
 */

exports.SpinnerDecorator = SpinnerDecorator;
var Spinner = SpinnerDecorator(SpinnerBase);
exports.Spinner = Spinner;
var _default = Spinner;
exports.default = _default;