"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SiteHubMobile = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _data = require("@wordpress/data");
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _coreData = require("@wordpress/core-data");
var _htmlEntities = require("@wordpress/html-entities");
var _element = require("@wordpress/element");
var _icons = require("@wordpress/icons");
var _commands = require("@wordpress/commands");
var _keycodes = require("@wordpress/keycodes");
var _url = require("@wordpress/url");
var _router = require("@wordpress/router");
var _store = require("../../store");
var _siteIcon = _interopRequireDefault(require("../site-icon"));
var _lockUnlock = require("../../lock-unlock");
var _sidebar = require("../sidebar");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useLocation,
  useHistory
} = (0, _lockUnlock.unlock)(_router.privateApis);
const SiteHub = (0, _element.memo)((0, _element.forwardRef)(({
  isTransparent
}, ref) => {
  const {
    dashboardLink,
    homeUrl,
    siteTitle
  } = (0, _data.useSelect)(select => {
    const {
      getSettings
    } = (0, _lockUnlock.unlock)(select(_store.store));
    const {
      getEntityRecord
    } = select(_coreData.store);
    const _site = getEntityRecord('root', 'site');
    return {
      dashboardLink: getSettings().__experimentalDashboardLink,
      homeUrl: getEntityRecord('root', '__unstableBase')?.home,
      siteTitle: !_site?.title && !!_site?.url ? (0, _url.filterURLForDisplay)(_site?.url) : _site?.title
    };
  }, []);
  const {
    open: openCommandCenter
  } = (0, _data.useDispatch)(_commands.store);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: "edit-site-site-hub",
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
      justify: "flex-start",
      spacing: "0",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: (0, _clsx.default)('edit-site-site-hub__view-mode-toggle-container', {
          'has-transparent-background': isTransparent
        }),
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          __next40pxDefaultSize: true,
          ref: ref,
          href: dashboardLink,
          label: (0, _i18n.__)('Go to the Dashboard'),
          className: "edit-site-layout__view-mode-toggle",
          style: {
            transform: 'scale(0.5333) translateX(-4px)',
            // Offset to position the icon 12px from viewport edge
            borderRadius: 4
          },
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_siteIcon.default, {
            className: "edit-site-layout__view-mode-toggle-icon"
          })
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "edit-site-site-hub__title",
          children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.Button, {
            __next40pxDefaultSize: true,
            variant: "link",
            href: homeUrl,
            target: "_blank",
            children: [(0, _htmlEntities.decodeEntities)(siteTitle), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.VisuallyHidden, {
              as: "span",
              children: /* translators: accessibility text */
              (0, _i18n.__)('(opens in a new tab)')
            })]
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalHStack, {
          spacing: 0,
          expanded: false,
          className: "edit-site-site-hub__actions",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
            size: "compact",
            className: "edit-site-site-hub_toggle-command-center",
            icon: _icons.search,
            onClick: () => openCommandCenter(),
            label: (0, _i18n.__)('Open command palette'),
            shortcut: _keycodes.displayShortcut.primary('k')
          })
        })]
      })]
    })
  });
}));
var _default = exports.default = SiteHub;
const SiteHubMobile = exports.SiteHubMobile = (0, _element.memo)((0, _element.forwardRef)(({
  isTransparent
}, ref) => {
  const {
    path
  } = useLocation();
  const history = useHistory();
  const {
    navigate
  } = (0, _element.useContext)(_sidebar.SidebarNavigationContext);
  const {
    dashboardLink,
    homeUrl,
    siteTitle,
    isBlockTheme,
    isClassicThemeWithStyleBookSupport
  } = (0, _data.useSelect)(select => {
    const {
      getSettings
    } = (0, _lockUnlock.unlock)(select(_store.store));
    const {
      getEntityRecord,
      getCurrentTheme
    } = select(_coreData.store);
    const _site = getEntityRecord('root', 'site');
    const currentTheme = getCurrentTheme();
    const settings = getSettings();
    const supportsEditorStyles = currentTheme.theme_supports['editor-styles'];
    // This is a temp solution until the has_theme_json value is available for the current theme.
    const hasThemeJson = settings.supportsLayout;
    return {
      dashboardLink: settings.__experimentalDashboardLink,
      homeUrl: getEntityRecord('root', '__unstableBase')?.home,
      siteTitle: !_site?.title && !!_site?.url ? (0, _url.filterURLForDisplay)(_site?.url) : _site?.title,
      isBlockTheme: currentTheme?.is_block_theme,
      isClassicThemeWithStyleBookSupport: !currentTheme?.is_block_theme && (supportsEditorStyles || hasThemeJson)
    };
  }, []);
  const {
    open: openCommandCenter
  } = (0, _data.useDispatch)(_commands.store);
  let backPath;

  // If the current path is not the root page, find a page to back to.
  if (path !== '/') {
    if (isBlockTheme || isClassicThemeWithStyleBookSupport) {
      // If the current theme is a block theme or a classic theme that supports StyleBook,
      // back to the Design screen.
      backPath = '/';
    } else if (path !== '/pattern') {
      // If the current theme is a classic theme that does not support StyleBook,
      // back to the Patterns page.
      backPath = '/pattern';
    }
  }
  const backButtonProps = {
    href: !!backPath ? undefined : dashboardLink,
    label: !!backPath ? (0, _i18n.__)('Go to Site Editor') : (0, _i18n.__)('Go to the Dashboard'),
    onClick: !!backPath ? () => {
      history.navigate(backPath);
      navigate('back');
    } : undefined
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: "edit-site-site-hub",
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
      justify: "flex-start",
      spacing: "0",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: (0, _clsx.default)('edit-site-site-hub__view-mode-toggle-container', {
          'has-transparent-background': isTransparent
        }),
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          __next40pxDefaultSize: true,
          ref: ref,
          className: "edit-site-layout__view-mode-toggle",
          style: {
            transform: 'scale(0.5)',
            borderRadius: 4
          },
          ...backButtonProps,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_siteIcon.default, {
            className: "edit-site-layout__view-mode-toggle-icon"
          })
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "edit-site-site-hub__title",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
            __next40pxDefaultSize: true,
            variant: "link",
            href: homeUrl,
            target: "_blank",
            label: (0, _i18n.__)('View site (opens in a new tab)'),
            children: (0, _htmlEntities.decodeEntities)(siteTitle)
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalHStack, {
          spacing: 0,
          expanded: false,
          className: "edit-site-site-hub__actions",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
            __next40pxDefaultSize: true,
            className: "edit-site-site-hub_toggle-command-center",
            icon: _icons.search,
            onClick: () => openCommandCenter(),
            label: (0, _i18n.__)('Open command palette'),
            shortcut: _keycodes.displayShortcut.primary('k')
          })
        })]
      })]
    })
  });
}));
//# sourceMappingURL=index.js.map