import { jsx, jsxs } from "react/jsx-runtime";
import { Page } from "@wordpress/admin-ui";
import { __ } from "@wordpress/i18n";
import { useSelect } from "@wordpress/data";
import { store as coreStore } from "@wordpress/core-data";
import { useMemo, useState } from "@wordpress/element";
import { privateApis as routerPrivateApis } from "@wordpress/router";
import { useViewportMatch } from "@wordpress/compose";
import {
  Button,
  __experimentalHStack as HStack,
  DropdownMenu,
  MenuGroup,
  MenuItem
} from "@wordpress/components";
import { addQueryArgs, removeQueryArgs } from "@wordpress/url";
import { seen, moreVertical } from "@wordpress/icons";
import GlobalStylesUI from "../global-styles/ui";
import { unlock } from "../../lock-unlock";
const { useLocation, useHistory } = unlock(routerPrivateApis);
const GlobalStylesPageActions = ({
  isStyleBookOpened,
  setIsStyleBookOpened,
  path
}) => {
  const history = useHistory();
  const canEditCSS = useSelect((select) => {
    const { getEntityRecord, __experimentalGetCurrentGlobalStylesId } = select(coreStore);
    const globalStylesId = __experimentalGetCurrentGlobalStylesId();
    const globalStyles = globalStylesId ? getEntityRecord("root", "globalStyles", globalStylesId) : void 0;
    return !!globalStyles?._links?.["wp:action-edit-css"];
  }, []);
  return /* @__PURE__ */ jsxs(HStack, { children: [
    /* @__PURE__ */ jsx(
      Button,
      {
        isPressed: isStyleBookOpened,
        icon: seen,
        label: __("Style Book"),
        onClick: () => {
          setIsStyleBookOpened(!isStyleBookOpened);
          const updatedPath = !isStyleBookOpened ? addQueryArgs(path, { preview: "stylebook" }) : removeQueryArgs(path, "preview");
          history.navigate(updatedPath);
        },
        size: "compact"
      }
    ),
    canEditCSS && /* @__PURE__ */ jsx(
      DropdownMenu,
      {
        icon: moreVertical,
        label: __("More"),
        toggleProps: { size: "compact" },
        children: ({ onClose }) => /* @__PURE__ */ jsx(MenuGroup, { children: canEditCSS && /* @__PURE__ */ jsx(
          MenuItem,
          {
            onClick: () => {
              onClose();
              history.navigate(
                addQueryArgs(path, {
                  section: "/css"
                })
              );
            },
            children: __("Additional CSS")
          }
        ) })
      }
    )
  ] });
};
const useSection = () => {
  const { path, query } = useLocation();
  const history = useHistory();
  return useMemo(() => {
    return [
      query.section ?? "/",
      (updatedSection) => {
        history.navigate(
          addQueryArgs(path, {
            section: updatedSection
          })
        );
      }
    ];
  }, [path, query.section, history]);
};
function GlobalStylesUIWrapper() {
  const { path } = useLocation();
  const [isStyleBookOpened, setIsStyleBookOpened] = useState(
    path.includes("preview=stylebook")
  );
  const isMobileViewport = useViewportMatch("medium", "<");
  const [section, onChangeSection] = useSection();
  return /* @__PURE__ */ jsx(
    Page,
    {
      actions: !isMobileViewport ? /* @__PURE__ */ jsx(
        GlobalStylesPageActions,
        {
          isStyleBookOpened,
          setIsStyleBookOpened,
          path
        }
      ) : null,
      className: "edit-site-styles",
      title: __("Styles"),
      children: /* @__PURE__ */ jsx(GlobalStylesUI, { path: section, onPathChange: onChangeSection })
    }
  );
}
export {
  GlobalStylesUIWrapper as default,
  useSection
};
//# sourceMappingURL=index.js.map
