/* wp:polyfill */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _blockEditor = require("@wordpress/block-editor");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _icons = require("@wordpress/icons");
var _keyboardShortcuts = require("@wordpress/keyboard-shortcuts");
var _components = require("@wordpress/components");
var _interface = require("@wordpress/interface");
var _patternOverridesPanel = _interopRequireDefault(require("../pattern-overrides-panel"));
var _pluginDocumentSettingPanel = _interopRequireDefault(require("../plugin-document-setting-panel"));
var _pluginSidebar = _interopRequireDefault(require("../plugin-sidebar"));
var _postSummary = _interopRequireDefault(require("./post-summary"));
var _panel = _interopRequireDefault(require("../post-taxonomies/panel"));
var _postTransformPanel = _interopRequireDefault(require("../post-transform-panel"));
var _header = _interopRequireDefault(require("./header"));
var _templateContentPanel = _interopRequireDefault(require("../template-content-panel"));
var _templatePartContentPanel = _interopRequireDefault(require("../template-part-content-panel"));
var _useAutoSwitchEditorSidebars = _interopRequireDefault(require("../provider/use-auto-switch-editor-sidebars"));
var _constants = require("./constants");
var _lockUnlock = require("../../lock-unlock");
var _store = require("../../store");
var _constants2 = require("../../store/constants");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  Tabs
} = (0, _lockUnlock.unlock)(_components.privateApis);
const SIDEBAR_ACTIVE_BY_DEFAULT = _element.Platform.select({
  web: true,
  native: false
});
const SidebarContent = ({
  tabName,
  keyboardShortcut,
  onActionPerformed,
  extraPanels
}) => {
  const tabListRef = (0, _element.useRef)(null);
  // Because `PluginSidebar` renders a `ComplementaryArea`, we
  // need to forward the `Tabs` context so it can be passed through the
  // underlying slot/fill.
  const tabsContextValue = (0, _element.useContext)(Tabs.Context);

  // This effect addresses a race condition caused by tabbing from the last
  // block in the editor into the settings sidebar. Without this effect, the
  // selected tab and browser focus can become separated in an unexpected way
  // (e.g the "block" tab is focused, but the "post" tab is selected).
  (0, _element.useEffect)(() => {
    const tabsElements = Array.from(tabListRef.current?.querySelectorAll('[role="tab"]') || []);
    const selectedTabElement = tabsElements.find(
    // We are purposefully using a custom `data-tab-id` attribute here
    // because we don't want rely on any assumptions about `Tabs`
    // component internals.
    element => element.getAttribute('data-tab-id') === tabName);
    const activeElement = selectedTabElement?.ownerDocument.activeElement;
    const tabsHasFocus = tabsElements.some(element => {
      return activeElement && activeElement.id === element.id;
    });
    if (tabsHasFocus && selectedTabElement && selectedTabElement.id !== activeElement?.id) {
      selectedTabElement?.focus();
    }
  }, [tabName]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_pluginSidebar.default, {
    identifier: tabName,
    header: /*#__PURE__*/(0, _jsxRuntime.jsx)(Tabs.Context.Provider, {
      value: tabsContextValue,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_header.default, {
        ref: tabListRef
      })
    }),
    closeLabel: (0, _i18n.__)('Close Settings')
    // This classname is added so we can apply a corrective negative
    // margin to the panel.
    // see https://github.com/WordPress/gutenberg/pull/55360#pullrequestreview-1737671049
    ,
    className: "editor-sidebar__panel",
    headerClassName: "editor-sidebar__panel-tabs",
    title: /* translators: button label text should, if possible, be under 16 characters. */
    (0, _i18n._x)('Settings', 'sidebar button label'),
    toggleShortcut: keyboardShortcut,
    icon: (0, _i18n.isRTL)() ? _icons.drawerLeft : _icons.drawerRight,
    isActiveByDefault: SIDEBAR_ACTIVE_BY_DEFAULT,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(Tabs.Context.Provider, {
      value: tabsContextValue,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(Tabs.TabPanel, {
        tabId: _constants.sidebars.document,
        focusable: false,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_postSummary.default, {
          onActionPerformed: onActionPerformed
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_pluginDocumentSettingPanel.default.Slot, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_templateContentPanel.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_templatePartContentPanel.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_postTransformPanel.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_panel.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_patternOverridesPanel.default, {}), extraPanels]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(Tabs.TabPanel, {
        tabId: _constants.sidebars.block,
        focusable: false,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.BlockInspector, {})
      })]
    })
  });
};
const Sidebar = ({
  extraPanels,
  onActionPerformed
}) => {
  (0, _useAutoSwitchEditorSidebars.default)();
  const {
    tabName,
    keyboardShortcut,
    showSummary
  } = (0, _data.useSelect)(select => {
    const shortcut = select(_keyboardShortcuts.store).getShortcutRepresentation('core/editor/toggle-sidebar');
    const sidebar = select(_interface.store).getActiveComplementaryArea('core');
    const _isEditorSidebarOpened = [_constants.sidebars.block, _constants.sidebars.document].includes(sidebar);
    let _tabName = sidebar;
    if (!_isEditorSidebarOpened) {
      _tabName = !!select(_blockEditor.store).getBlockSelectionStart() ? _constants.sidebars.block : _constants.sidebars.document;
    }
    return {
      tabName: _tabName,
      keyboardShortcut: shortcut,
      showSummary: ![_constants2.TEMPLATE_POST_TYPE, _constants2.TEMPLATE_PART_POST_TYPE, _constants2.NAVIGATION_POST_TYPE].includes(select(_store.store).getCurrentPostType())
    };
  }, []);
  const {
    enableComplementaryArea
  } = (0, _data.useDispatch)(_interface.store);
  const onTabSelect = (0, _element.useCallback)(newSelectedTabId => {
    if (!!newSelectedTabId) {
      enableComplementaryArea('core', newSelectedTabId);
    }
  }, [enableComplementaryArea]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Tabs, {
    selectedTabId: tabName,
    onSelect: onTabSelect,
    selectOnMove: false,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(SidebarContent, {
      tabName: tabName,
      keyboardShortcut: keyboardShortcut,
      showSummary: showSummary,
      onActionPerformed: onActionPerformed,
      extraPanels: extraPanels
    })
  });
};
var _default = exports.default = Sidebar;
//# sourceMappingURL=index.js.map