"use strict";

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

var _jsxRuntime = require("react/jsx-runtime");

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

var _react = require("react");

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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 _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, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

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

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

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

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 (_Component) {
    _inherits(_class, _Component);

    var _super = _createSuper(_class);

    function _class() {
      var _this;

      _classCallCheck(this, _class);

      _this = _super.call(this);

      _this.generateId = function (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 _this$ids$key = this.ids[key],
              id = _this$ids$key.id,
              onUnmount = _this$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 /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread({}, props));
      }
    }]);

    return _class;
  }(_react.Component), _class.displayName = 'IdProvider', _temp;
});
exports.IdProvider = IdProvider;
var _default = IdProvider;
exports["default"] = _default;