"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useCommonCommands = useCommonCommands;
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _i18n = require("@wordpress/i18n");
var _icons = require("@wordpress/icons");
var _commands = require("@wordpress/commands");
var _blockEditor = require("@wordpress/block-editor");
var _router = require("@wordpress/router");
var _preferences = require("@wordpress/preferences");
var _coreData = require("@wordpress/core-data");
var _lockUnlock = require("../../lock-unlock");
var _store = require("../../store");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useGlobalStylesReset
} = (0, _lockUnlock.unlock)(_blockEditor.privateApis);
const {
  useHistory,
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);
const getGlobalStylesOpenStylesCommands = () => function useGlobalStylesOpenStylesCommands() {
  const {
    openGeneralSidebar
  } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store));
  const {
    params
  } = useLocation();
  const {
    canvas = 'view'
  } = params;
  const history = useHistory();
  const isBlockBasedTheme = (0, _data.useSelect)(select => {
    return select(_coreData.store).getCurrentTheme().is_block_theme;
  }, []);
  const commands = (0, _element.useMemo)(() => {
    if (!isBlockBasedTheme) {
      return [];
    }
    return [{
      name: 'core/edit-site/open-styles',
      label: (0, _i18n.__)('Open styles'),
      callback: ({
        close
      }) => {
        close();
        if (canvas !== 'edit') {
          history.navigate('/styles?canvas=edit', {
            transition: 'canvas-mode-edit-transition'
          });
        }
        openGeneralSidebar('edit-site/global-styles');
      },
      icon: _icons.styles
    }];
  }, [history, openGeneralSidebar, canvas, isBlockBasedTheme]);
  return {
    isLoading: false,
    commands
  };
};
const getGlobalStylesToggleWelcomeGuideCommands = () => function useGlobalStylesToggleWelcomeGuideCommands() {
  const {
    openGeneralSidebar
  } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store));
  const {
    params
  } = useLocation();
  const {
    canvas = 'view'
  } = params;
  const {
    set
  } = (0, _data.useDispatch)(_preferences.store);
  const history = useHistory();
  const isBlockBasedTheme = (0, _data.useSelect)(select => {
    return select(_coreData.store).getCurrentTheme().is_block_theme;
  }, []);
  const commands = (0, _element.useMemo)(() => {
    if (!isBlockBasedTheme) {
      return [];
    }
    return [{
      name: 'core/edit-site/toggle-styles-welcome-guide',
      label: (0, _i18n.__)('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);
        // sometimes there's a focus loss that happens after some time
        // that closes the modal, we need to force reopening it.
        setTimeout(() => {
          set('core/edit-site', 'welcomeGuideStyles', true);
        }, 500);
      },
      icon: _icons.help
    }];
  }, [history, openGeneralSidebar, canvas, isBlockBasedTheme, set]);
  return {
    isLoading: false,
    commands
  };
};
const getGlobalStylesResetCommands = () => function useGlobalStylesResetCommands() {
  const [canReset, onReset] = useGlobalStylesReset();
  const commands = (0, _element.useMemo)(() => {
    if (!canReset) {
      return [];
    }
    return [{
      name: 'core/edit-site/reset-global-styles',
      label: (0, _i18n.__)('Reset styles'),
      icon: (0, _i18n.isRTL)() ? _icons.rotateRight : _icons.rotateLeft,
      callback: ({
        close
      }) => {
        close();
        onReset();
      }
    }];
  }, [canReset, onReset]);
  return {
    isLoading: false,
    commands
  };
};
const getGlobalStylesOpenCssCommands = () => function useGlobalStylesOpenCssCommands() {
  const {
    openGeneralSidebar,
    setEditorCanvasContainerView
  } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store));
  const {
    params
  } = useLocation();
  const {
    canvas = 'view'
  } = params;
  const history = useHistory();
  const {
    canEditCSS
  } = (0, _data.useSelect)(select => {
    const {
      getEntityRecord,
      __experimentalGetCurrentGlobalStylesId
    } = select(_coreData.store);
    const globalStylesId = __experimentalGetCurrentGlobalStylesId();
    const globalStyles = globalStylesId ? getEntityRecord('root', 'globalStyles', globalStylesId) : undefined;
    return {
      canEditCSS: !!globalStyles?._links?.['wp:action-edit-css']
    };
  }, []);
  const commands = (0, _element.useMemo)(() => {
    if (!canEditCSS) {
      return [];
    }
    return [{
      name: 'core/edit-site/open-styles-css',
      label: (0, _i18n.__)('Customize CSS'),
      icon: _icons.brush,
      callback: ({
        close
      }) => {
        close();
        if (canvas !== 'edit') {
          history.navigate('/styles?canvas=edit', {
            transition: 'canvas-mode-edit-transition'
          });
        }
        openGeneralSidebar('edit-site/global-styles');
        setEditorCanvasContainerView('global-styles-css');
      }
    }];
  }, [history, openGeneralSidebar, setEditorCanvasContainerView, canEditCSS, canvas]);
  return {
    isLoading: false,
    commands
  };
};
const getGlobalStylesOpenRevisionsCommands = () => function useGlobalStylesOpenRevisionsCommands() {
  const {
    openGeneralSidebar,
    setEditorCanvasContainerView
  } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store));
  const {
    params
  } = useLocation();
  const {
    canvas = 'view'
  } = params;
  const history = useHistory();
  const hasRevisions = (0, _data.useSelect)(select => {
    const {
      getEntityRecord,
      __experimentalGetCurrentGlobalStylesId
    } = select(_coreData.store);
    const globalStylesId = __experimentalGetCurrentGlobalStylesId();
    const globalStyles = globalStylesId ? getEntityRecord('root', 'globalStyles', globalStylesId) : undefined;
    return !!globalStyles?._links?.['version-history']?.[0]?.count;
  }, []);
  const commands = (0, _element.useMemo)(() => {
    if (!hasRevisions) {
      return [];
    }
    return [{
      name: 'core/edit-site/open-global-styles-revisions',
      label: (0, _i18n.__)('Style revisions'),
      icon: _icons.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');
      }
    }];
  }, [hasRevisions, history, openGeneralSidebar, setEditorCanvasContainerView, canvas]);
  return {
    isLoading: false,
    commands
  };
};
function useCommonCommands() {
  const homeUrl = (0, _data.useSelect)(select => {
    // Site index.
    return select(_coreData.store).getEntityRecord('root', '__unstableBase')?.home;
  }, []);
  (0, _commands.useCommand)({
    name: 'core/edit-site/view-site',
    label: (0, _i18n.__)('View site'),
    callback: ({
      close
    }) => {
      close();
      window.open(homeUrl, '_blank');
    },
    icon: _icons.external
  });
  (0, _commands.useCommandLoader)({
    name: 'core/edit-site/open-styles',
    hook: getGlobalStylesOpenStylesCommands()
  });
  (0, _commands.useCommandLoader)({
    name: 'core/edit-site/toggle-styles-welcome-guide',
    hook: getGlobalStylesToggleWelcomeGuideCommands()
  });
  (0, _commands.useCommandLoader)({
    name: 'core/edit-site/reset-global-styles',
    hook: getGlobalStylesResetCommands()
  });
  (0, _commands.useCommandLoader)({
    name: 'core/edit-site/open-styles-css',
    hook: getGlobalStylesOpenCssCommands()
  });
  (0, _commands.useCommandLoader)({
    name: 'core/edit-site/open-styles-revisions',
    hook: getGlobalStylesOpenRevisionsCommands()
  });
}
//# sourceMappingURL=use-common-commands.js.map