"use strict";

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

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

var _invariant = _interopRequireDefault(require("invariant"));

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

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

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

var _Router = require("./Router");

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

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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

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; }

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; }

/**
 * Default config for [`Routable`]{@link moonstone/Panels.Routable}.
 *
 * @memberof moonstone/Panels.Routable
 * @hocconfig
 */
var defaultConfig = {
  /**
   * The event to listen to for path changes.
   *
   * This defines the actual name of the [navigate]{@link moonstone/Panels.Routable#navigate}
   * property.
   *
   * @type {String}
   * @required
   * @memberof moonstone/Panels.Routable.defaultConfig
   */
  navigate: null
};
/**
 * A higher-order component that provides support for Routes as children of Panels which are
 * selected via `path` instead of the usual flat array of Panels.
 *
 * When using `Routable` you must specify the `navigate` config option.
 *
 * @class Routable
 * @memberof moonstone/Panels
 * @hoc
 * @public
 */

var Routable = (0, _hoc.default)(defaultConfig, function (config, Wrapped) {
  var navigate = config.navigate;
  !navigate ? process.env.NODE_ENV !== "production" ? (0, _invariant.default)(false, 'navigate must be specified with Routable') : invariant(false) : void 0;
  return (0, _kind.default)({
    name: 'Routable',
    propTypes:
    /** @lends moonstone/Panels.Routable.prototype */
    _defineProperty({
      /**
       * Path to the active panel.
       *
       * May either be a URI-style path (`'/app/home/settings'`) or an array
       * of strings (`['app', 'home', 'settings']`).
       *
       * @type {String|String[]}
       * @required
       * @public
       */
      path: _Router.propTypes.path.isRequired
    }, navigate, _propTypes2.default.func),
    handlers: _defineProperty({}, navigate, function (_ref, _ref2) {
      var path = _ref2.path,
          handler = _ref2[navigate];

      var index = _ref.index,
          rest = _objectWithoutProperties(_ref, ["index"]);

      if (handler) {
        var p = (0, _Router.toSegments)(path).slice(0, index + 1);
        handler(_objectSpread({}, rest, {
          index: index,
          path: Array.isArray(path) ? p : '/' + p.join('/')
        }));
      }
    }),
    computed: {
      // Determines the `index` as 1 less than the number of segments in the path
      index: function index(_ref3) {
        var path = _ref3.path;
        return (0, _Router.toSegments)(path).length - 1;
      }
    },
    render: function render(_ref4) {
      var children = _ref4.children,
          index = _ref4.index,
          path = _ref4.path,
          rest = _objectWithoutProperties(_ref4, ["children", "index", "path"]);

      return _react.default.createElement(_Router.Router, _extends({}, rest, {
        path: path,
        component: Wrapped,
        index: index
      }), children);
    }
  });
});
exports.Routable = Routable;
var _default = Routable;
exports.default = _default;