"use strict";

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

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

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"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; 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 _objectWithoutPropertiesLoose(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]; } return target; }

var panelId = 0;
/**
 * A Panel is the standard view container used inside a [Panels]{@link agate/Panels.Panels} view
 * manager instance. [Panels]{@link agate/Panels.Panels} will typically contain several
 * instances of these and transition between them.
 *
 * @class Panel
 * @memberof agate/Panels
 * @ui
 * @public
 */

var PanelBase = (0, _kind["default"])({
  name: 'Panel',
  propTypes:
  /** @lends agate/Panels.Panel.prototype */
  {
    /**
     * By default, the panel will be labeled by its [Header]{@link agate/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 [Slottable]{@link ui/Slottable.Slottable} API by using a
     * [Header]{@link agate/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 [`Panels`]{@link agate/Panels.Panels},
     * [`BreadcrumbPanels`]{@link agate/Panels.BreadcrumbPanels}, or
     * [`TabbedPanels`]{@link agate/Panels.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, ["bodyClassName", "css", "children", "header", "headerId", "spotOnRender"]);

    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
      })]
    }));
  }
});
exports.PanelBase = PanelBase;
var 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)));
exports.Panel = Panel;
var _default = Panel;
exports["default"] = _default;