"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var global_styles_sidebar_exports = {};
__export(global_styles_sidebar_exports, {
  default: () => GlobalStylesSidebar
});
module.exports = __toCommonJS(global_styles_sidebar_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_components = require("@wordpress/components");
var import_i18n = require("@wordpress/i18n");
var import_icons = require("@wordpress/icons");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_preferences = require("@wordpress/preferences");
var import_editor = require("@wordpress/editor");
var import_compose = require("@wordpress/compose");
var import_router = require("@wordpress/router");
var import_global_styles = require("../global-styles");
var import_store = require("../../store");
var import_ui = require("../global-styles/ui");
var import_lock_unlock = require("../../lock-unlock");
var import_core_data = require("@wordpress/core-data");
var import_default_sidebar = __toESM(require("./default-sidebar"));
const { interfaceStore } = (0, import_lock_unlock.unlock)(import_editor.privateApis);
const { useLocation } = (0, import_lock_unlock.unlock)(import_router.privateApis);
function GlobalStylesSidebar() {
  const { query } = useLocation();
  const { canvas = "view", name } = query;
  const {
    shouldClearCanvasContainerView,
    isStyleBookOpened,
    showListViewByDefault,
    hasRevisions,
    isRevisionsOpened,
    isRevisionsStyleBookOpened
  } = (0, import_data.useSelect)(
    (select) => {
      const { getActiveComplementaryArea: getActiveComplementaryArea2 } = select(interfaceStore);
      const { getEditorCanvasContainerView } = (0, import_lock_unlock.unlock)(
        select(import_store.store)
      );
      const canvasContainerView = getEditorCanvasContainerView();
      const _isVisualEditorMode = "visual" === select(import_editor.store).getEditorMode();
      const _isEditCanvasMode = "edit" === canvas;
      const _showListViewByDefault = select(import_preferences.store).get(
        "core",
        "showListViewByDefault"
      );
      const { getEntityRecord, __experimentalGetCurrentGlobalStylesId } = select(import_core_data.store);
      const globalStylesId = __experimentalGetCurrentGlobalStylesId();
      const globalStyles = globalStylesId ? getEntityRecord("root", "globalStyles", globalStylesId) : void 0;
      return {
        isStyleBookOpened: "style-book" === canvasContainerView,
        shouldClearCanvasContainerView: "edit-site/global-styles" !== getActiveComplementaryArea2("core") || !_isVisualEditorMode || !_isEditCanvasMode,
        showListViewByDefault: _showListViewByDefault,
        hasRevisions: !!globalStyles?._links?.["version-history"]?.[0]?.count,
        isRevisionsStyleBookOpened: "global-styles-revisions:style-book" === canvasContainerView,
        isRevisionsOpened: "global-styles-revisions" === canvasContainerView
      };
    },
    [canvas]
  );
  const { setEditorCanvasContainerView } = (0, import_lock_unlock.unlock)(
    (0, import_data.useDispatch)(import_store.store)
  );
  const isMobileViewport = (0, import_compose.useViewportMatch)("medium", "<");
  (0, import_element.useEffect)(() => {
    if (shouldClearCanvasContainerView) {
      setEditorCanvasContainerView(void 0);
    }
  }, [shouldClearCanvasContainerView, setEditorCanvasContainerView]);
  const { setIsListViewOpened } = (0, import_data.useDispatch)(import_editor.store);
  const toggleRevisions = () => {
    setIsListViewOpened(false);
    if (isRevisionsStyleBookOpened) {
      setEditorCanvasContainerView("style-book");
      return;
    }
    if (isRevisionsOpened) {
      setEditorCanvasContainerView(void 0);
      return;
    }
    if (isStyleBookOpened) {
      setEditorCanvasContainerView(
        "global-styles-revisions:style-book"
      );
    } else {
      setEditorCanvasContainerView("global-styles-revisions");
    }
  };
  const toggleStyleBook = () => {
    if (isRevisionsOpened) {
      setEditorCanvasContainerView(
        "global-styles-revisions:style-book"
      );
      return;
    }
    if (isRevisionsStyleBookOpened) {
      setEditorCanvasContainerView("global-styles-revisions");
      return;
    }
    setIsListViewOpened(isStyleBookOpened && showListViewByDefault);
    setEditorCanvasContainerView(
      isStyleBookOpened ? void 0 : "style-book"
    );
  };
  const { getActiveComplementaryArea } = (0, import_data.useSelect)(interfaceStore);
  const { enableComplementaryArea } = (0, import_data.useDispatch)(interfaceStore);
  const previousActiveAreaRef = (0, import_element.useRef)(null);
  (0, import_element.useEffect)(() => {
    if (name === "styles" && canvas === "edit") {
      previousActiveAreaRef.current = getActiveComplementaryArea("core");
      enableComplementaryArea("core", "edit-site/global-styles");
    } else if (previousActiveAreaRef.current) {
      enableComplementaryArea("core", previousActiveAreaRef.current);
    }
  }, [name, enableComplementaryArea, canvas, getActiveComplementaryArea]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_default_sidebar.default,
    {
      className: "edit-site-global-styles-sidebar",
      identifier: "edit-site/global-styles",
      title: (0, import_i18n.__)("Styles"),
      icon: import_icons.styles,
      closeLabel: (0, import_i18n.__)("Close Styles"),
      panelClassName: "edit-site-global-styles-sidebar__panel",
      header: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
        import_components.Flex,
        {
          className: "edit-site-global-styles-sidebar__header",
          gap: 1,
          children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.FlexItem, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h2", { className: "edit-site-global-styles-sidebar__header-title", children: (0, import_i18n.__)("Styles") }) }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
              import_components.Flex,
              {
                justify: "flex-end",
                gap: 1,
                className: "edit-site-global-styles-sidebar__header-actions",
                children: [
                  !isMobileViewport && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.FlexItem, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                    import_components.Button,
                    {
                      icon: import_icons.seen,
                      label: (0, import_i18n.__)("Style Book"),
                      isPressed: isStyleBookOpened || isRevisionsStyleBookOpened,
                      accessibleWhenDisabled: true,
                      disabled: shouldClearCanvasContainerView,
                      onClick: toggleStyleBook,
                      size: "compact"
                    }
                  ) }),
                  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.FlexItem, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                    import_components.Button,
                    {
                      label: (0, import_i18n.__)("Revisions"),
                      icon: import_icons.backup,
                      onClick: toggleRevisions,
                      accessibleWhenDisabled: true,
                      disabled: !hasRevisions,
                      isPressed: isRevisionsOpened || isRevisionsStyleBookOpened,
                      size: "compact"
                    }
                  ) }),
                  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.GlobalStylesMenuSlot, {})
                ]
              }
            )
          ]
        }
      ),
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_global_styles.GlobalStylesUI, {})
    }
  );
}
//# sourceMappingURL=index.js.map
