import { useMemo } from "@wordpress/element";
import { useDispatch, useSelect } from "@wordpress/data";
import { __, isRTL } from "@wordpress/i18n";
import { rotateLeft, rotateRight, help, backup } from "@wordpress/icons";
import { useCommandLoader } from "@wordpress/commands";
import { privateApis as blockEditorPrivateApis } from "@wordpress/block-editor";
import { privateApis as routerPrivateApis } from "@wordpress/router";
import { store as preferencesStore } from "@wordpress/preferences";
import { store as coreStore } from "@wordpress/core-data";
import { unlock } from "../../lock-unlock";
import { store as editSiteStore } from "../../store";
const { useGlobalStylesReset } = unlock(blockEditorPrivateApis);
const { useHistory, useLocation } = unlock(routerPrivateApis);
const getGlobalStylesToggleWelcomeGuideCommands = () => function useGlobalStylesToggleWelcomeGuideCommands() {
  const { openGeneralSidebar } = unlock(useDispatch(editSiteStore));
  const { params } = useLocation();
  const { canvas = "view" } = params;
  const { set } = useDispatch(preferencesStore);
  const history = useHistory();
  const isBlockBasedTheme = useSelect((select) => {
    return select(coreStore).getCurrentTheme().is_block_theme;
  }, []);
  const commands = useMemo(() => {
    if (!isBlockBasedTheme) {
      return [];
    }
    return [
      {
        name: "core/edit-site/toggle-styles-welcome-guide",
        label: __("Learn about styles"),
        callback: ({ close }) => {
          close();
          if (canvas !== "edit") {
            history.navigate("/styles?canvas=edit", {
              transition: "canvas-mode-edit-transition"
            });
          }
          openGeneralSidebar("edit-site/global-styles");
          set("core/edit-site", "welcomeGuideStyles", true);
          setTimeout(() => {
            set("core/edit-site", "welcomeGuideStyles", true);
          }, 500);
        },
        icon: help
      }
    ];
  }, [history, openGeneralSidebar, canvas, isBlockBasedTheme, set]);
  return {
    isLoading: false,
    commands
  };
};
const getGlobalStylesResetCommands = () => function useGlobalStylesResetCommands() {
  const [canReset, onReset] = useGlobalStylesReset();
  const commands = useMemo(() => {
    if (!canReset) {
      return [];
    }
    return [
      {
        name: "core/edit-site/reset-global-styles",
        label: __("Reset styles"),
        icon: isRTL() ? rotateRight : rotateLeft,
        callback: ({ close }) => {
          close();
          onReset();
        }
      }
    ];
  }, [canReset, onReset]);
  return {
    isLoading: false,
    commands
  };
};
const getGlobalStylesOpenRevisionsCommands = () => function useGlobalStylesOpenRevisionsCommands() {
  const { openGeneralSidebar, setEditorCanvasContainerView } = unlock(
    useDispatch(editSiteStore)
  );
  const { params } = useLocation();
  const { canvas = "view" } = params;
  const history = useHistory();
  const hasRevisions = useSelect((select) => {
    const { getEntityRecord, __experimentalGetCurrentGlobalStylesId } = select(coreStore);
    const globalStylesId = __experimentalGetCurrentGlobalStylesId();
    const globalStyles = globalStylesId ? getEntityRecord("root", "globalStyles", globalStylesId) : void 0;
    return !!globalStyles?._links?.["version-history"]?.[0]?.count;
  }, []);
  const commands = useMemo(() => {
    if (!hasRevisions) {
      return [];
    }
    return [
      {
        name: "core/edit-site/open-styles-revisions",
        label: __("Open style revisions"),
        icon: backup,
        callback: ({ close }) => {
          close();
          if (canvas !== "edit") {
            history.navigate("/styles?canvas=edit", {
              transition: "canvas-mode-edit-transition"
            });
          }
          openGeneralSidebar("edit-site/global-styles");
          setEditorCanvasContainerView(
            "global-styles-revisions"
          );
        }
      }
    ];
  }, [
    history,
    openGeneralSidebar,
    setEditorCanvasContainerView,
    hasRevisions,
    canvas
  ]);
  return {
    isLoading: false,
    commands
  };
};
function useCommonCommands() {
  useCommandLoader({
    name: "core/edit-site/toggle-styles-welcome-guide",
    hook: getGlobalStylesToggleWelcomeGuideCommands()
  });
  useCommandLoader({
    name: "core/edit-site/reset-global-styles",
    hook: getGlobalStylesResetCommands()
  });
  useCommandLoader({
    name: "core/edit-site/open-styles-revisions",
    hook: getGlobalStylesOpenRevisionsCommands()
  });
}
export {
  useCommonCommands
};
//# sourceMappingURL=use-common-commands.js.map
