"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.PanelBase = exports.Panel = void 0;
var _handle = require("@enact/core/handle");
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _Slottable = _interopRequireDefault(require("@enact/ui/Slottable"));
var _spotlight = _interopRequireDefault(require("@enact/spotlight"));
var _SpotlightContainerDecorator = _interopRequireWildcard(require("@enact/spotlight/SpotlightContainerDecorator"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _Skinnable = _interopRequireDefault(require("../Skinnable"));
var _PanelModule = _interopRequireDefault(require("./Panel.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["bodyClassName", "css", "children", "header", "headerId", "spotOnRender"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
var panelId = 0;

/**
 * A Panel is the standard view container used inside a {@link agate/Panels.Panels|Panels} view
 * manager instance. {@link agate/Panels.Panels|Panels} will typically contain several
 * instances of these and transition between them.
 *
 * @class Panel
 * @memberof agate/Panels
 * @ui
 * @public
 */
var PanelBase = exports.PanelBase = (0, _kind["default"])({
  name: 'Panel',
  propTypes: /** @lends agate/Panels.Panel.prototype */{
    /**
     * By default, the panel will be labeled by its {@link agate/Header.Header|Header}.
     * When `aria-label` is set, it will be used instead to provide an accessibility label for
     * the panel.
     *
     * @memberof agate/Panels.Panel.prototype
     * @type {String}
     * @public
     */
    'aria-label': _propTypes["default"].string,
    /**
     * Sets the strategy used to automatically focus an element within the panel upon render.
     *
     * * "none" - Automatic focus is disabled
     * * "last-focused" - The element last focused in the panel with be restored
     * * "default-element" - The first spottable component within the body will be focused
     * * Custom Selector - A custom CSS selector may also be provided which will be used to find
     *   the target within the Panel
     *
     * @type {String}
     * @default 'last-focused'
     * @public
     */
    autoFocus: _propTypes["default"].string,
    /**
     * 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:
     *
     * * `panel` - The root class name
     * * `body` - Applied to the <section> element that displays the `children` of the `Panel`
     * * `header` - Applied to the header element
     *
     * @type {Object}
     * @public
     */
    css: _propTypes["default"].object,
    /**
     * Header for the panel.
     *
     * This is usually passed by the {@link ui/Slottable.Slottable|Slottable} API by using a
     * {@link agate/Header.Header|Header} component as a child of the Panel.
     *
     * @type {Node}
     * @public
     */
    header: _propTypes["default"].node,
    /**
     * Hides the body components.
     *
     * When a Panel is used within {@link agate/Panels.Panels|Panels},
     * {@link agate/Panels.BreadcrumbPanels|BreadcrumbPanels}, or
     * {@link agate/Panels.TabbedPanels|TabbedPanels},
     * this property will be set automatically to `true` on render and `false` after animating
     * into view.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    hideChildren: _propTypes["default"].bool
  },
  defaultProps: {
    autoFocus: 'last-focused',
    hideChildren: false
  },
  styles: {
    css: _PanelModule["default"],
    className: 'panel',
    publicClassNames: true
  },
  handlers: {
    onScroll: (0, _handle.handle)((0, _handle.forward)('onScroll'), function (_ref) {
      var currentTarget = _ref.currentTarget;
      currentTarget.scrollTop = 0;
      currentTarget.scrollLeft = 0;
    }),
    spotOnRender: function spotOnRender(node, _ref2) {
      var autoFocus = _ref2.autoFocus;
      if (node && !_spotlight["default"].getCurrent()) {
        var spotlightId = node.dataset.spotlightId;
        var config = {
          enterTo: 'last-focused'
        };
        if (autoFocus !== 'last-focused') {
          config.enterTo = 'default-element';
          if (autoFocus !== 'default-element') {
            config.defaultElement = autoFocus;
          }
        }
        _spotlight["default"].set(spotlightId, config);
        _spotlight["default"].focus(spotlightId);
      }
    }
  },
  computed: {
    spotOnRender: function spotOnRender(_ref3) {
      var autoFocus = _ref3.autoFocus,
        hideChildren = _ref3.hideChildren,
        _spotOnRender = _ref3.spotOnRender;
      // In order to spot the body components, we defer spotting until !hideChildren. If the
      // Panel opts out of hideChildren support by explicitly setting it to false, it'll spot
      // on first render.
      if (hideChildren || autoFocus === 'none') {
        return null;
      }
      return _spotOnRender;
    },
    children: function children(_ref4) {
      var _children = _ref4.children,
        hideChildren = _ref4.hideChildren;
      return hideChildren ? null : _children;
    },
    bodyClassName: function bodyClassName(_ref5) {
      var header = _ref5.header,
        hideChildren = _ref5.hideChildren,
        styler = _ref5.styler;
      return styler.join({
        body: true,
        noHeader: !header,
        visible: !hideChildren
      });
    },
    // nulling headerId prevents the aria-labelledby relationship which is necessary to allow
    // aria-label to take precedence
    // (see https://www.w3.org/TR/wai-aria/states_and_properties#aria-labelledby)
    headerId: function headerId(_ref6) {
      var label = _ref6['aria-label'];
      return label ? null : "panel_".concat(++panelId, "_header");
    }
  },
  render: function render(_ref7) {
    var bodyClassName = _ref7.bodyClassName,
      css = _ref7.css,
      children = _ref7.children,
      header = _ref7.header,
      headerId = _ref7.headerId,
      spotOnRender = _ref7.spotOnRender,
      rest = _objectWithoutProperties(_ref7, _excluded);
    delete rest.autoFocus;
    delete rest.hideChildren;
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)("article", _objectSpread(_objectSpread({
      role: "region"
    }, rest), {}, {
      "aria-labelledby": headerId,
      ref: spotOnRender,
      children: [header ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: css.header,
        id: headerId,
        children: header
      }) : null, /*#__PURE__*/(0, _jsxRuntime.jsx)("section", {
        className: bodyClassName,
        children: children
      })]
    }));
  }
});
var Panel = exports.Panel = (0, _SpotlightContainerDecorator["default"])({
  // prefer any spottable within the panel body for first render
  continue5WayHold: true,
  defaultElement: [".".concat(_SpotlightContainerDecorator.spotlightDefaultClass), ".".concat(_PanelModule["default"].body, " *")],
  enterTo: 'last-focused',
  preserveId: true
}, (0, _Slottable["default"])({
  slots: ['header']
}, (0, _Skinnable["default"])(PanelBase)));
var _default = exports["default"] = Panel;