"use strict";

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

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

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

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 _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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

var GlobalId = 0;
var ID_KEY = '$$ID$$';
/**
 * Default config for {@link moonstone/Panels.IdProvider}
 *
 * @hocconfig
 * @memberof moonstone/Panels.IdProvider
 */

var defaultConfig = {
  /**
   * Prop to pass the identifier generation function
   *
   * @type {String}
   * @default generateId
   * @memberof moonstone/Panels.IdProvider.defaultConfig
   */
  generateProp: 'generateId',

  /**
   * Prop to pass the identifier
   *
   * @type {String}
   * @default id
   * @memberof moonstone/Panels.IdProvider.defaultConfig
   */
  idProp: 'id',

  /**
   * Optional prefix for the identifier
   *
   * @type {String}
   * @default 'c_'
   * @memberof moonstone/Panels.IdProvider.defaultConfig
   */
  prefix: 'c_'
};
/**
 * A higher-order component that generates globally-unique identifiers
 *
 * @class IdProvider
 * @hoc
 * @private
 * @memberof moonstone/Panels
 */

var IdProvider = (0, _hoc.default)(defaultConfig, function (config, Wrapped) {
  var _class, _temp;

  var generateProp = config.generateProp,
      idProp = config.idProp,
      prefix = config.prefix;
  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));
      Object.defineProperty(_assertThisInitialized(_this), "generateId", {
        configurable: true,
        enumerable: true,
        writable: true,
        value: function value(key) {
          var idPrefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : prefix;
          var onUnmount = arguments.length > 2 ? arguments[2] : undefined;

          // if an id has been generated for the key, return it
          if (key in _this.ids) {
            return _this.ids[key].id;
          } // otherwise generate a new id (with an optional prefix), cache it, and return it


          var id = "".concat(idPrefix).concat(++GlobalId);
          _this.ids[typeof key === 'undefined' ? "generated-".concat(id) : key] = {
            id: id,
            onUnmount: onUnmount
          };
          return id;
        }
      });
      _this.ids = {};
      return _this;
    }

    _createClass(_class, [{
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        // Call the onUnmount handler for each generated id (note: not the key)
        for (var key in this.ids) {
          var _ids$key = this.ids[key],
              id = _ids$key.id,
              onUnmount = _ids$key.onUnmount;

          if (typeof onUnmount === 'function') {
            onUnmount(id);
          }
        }
      }
    }, {
      key: "render",
      value: function render() {
        var props = Object.assign({}, this.props);

        if (generateProp) {
          props[generateProp] = this.generateId;
        }

        if (idProp && !props[idProp]) {
          props[idProp] = this.generateId(ID_KEY);
        }

        return _react.default.createElement(Wrapped, props);
      }
    }]);

    return _class;
  }(_react.default.Component), Object.defineProperty(_class, "displayName", {
    configurable: true,
    enumerable: true,
    writable: true,
    value: 'IdProvider'
  }), _temp;
});
exports.IdProvider = IdProvider;
var _default = IdProvider;
exports.default = _default;