"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useLayoutAreas;
var _router = require("@wordpress/router");
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _lockUnlock = require("../../lock-unlock");
var _editor = _interopRequireDefault(require("../editor"));
var _postList = _interopRequireDefault(require("../post-list"));
var _pagePatterns = _interopRequireDefault(require("../page-patterns"));
var _pageTemplates = _interopRequireDefault(require("../page-templates"));
var _sidebarNavigationScreen = _interopRequireDefault(require("../sidebar-navigation-screen"));
var _sidebarNavigationScreenGlobalStyles = _interopRequireDefault(require("../sidebar-navigation-screen-global-styles"));
var _sidebarNavigationScreenMain = _interopRequireDefault(require("../sidebar-navigation-screen-main"));
var _sidebarNavigationScreenNavigationMenus = _interopRequireDefault(require("../sidebar-navigation-screen-navigation-menus"));
var _sidebarNavigationScreenTemplatesBrowse = _interopRequireDefault(require("../sidebar-navigation-screen-templates-browse"));
var _sidebarNavigationScreenPatterns = _interopRequireDefault(require("../sidebar-navigation-screen-patterns"));
var _sidebarNavigationScreenNavigationMenu = _interopRequireDefault(require("../sidebar-navigation-screen-navigation-menu"));
var _sidebarDataviews = _interopRequireDefault(require("../sidebar-dataviews"));
var _constants = require("../../utils/constants");
var _postEdit = require("../post-edit");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useLocation,
  useHistory
} = (0, _lockUnlock.unlock)(_router.privateApis);
function useRedirectOldPaths() {
  const history = useHistory();
  const {
    params
  } = useLocation();
  (0, _element.useEffect)(() => {
    const {
      postType,
      path,
      categoryType,
      ...rest
    } = params;
    if (path === '/wp_template_part/all') {
      history.replace({
        postType: _constants.TEMPLATE_PART_POST_TYPE
      });
    }
    if (path === '/page') {
      history.replace({
        postType: 'page',
        ...rest
      });
    }
    if (path === '/wp_template') {
      history.replace({
        postType: _constants.TEMPLATE_POST_TYPE,
        ...rest
      });
    }
    if (path === '/patterns') {
      history.replace({
        postType: categoryType === _constants.TEMPLATE_PART_POST_TYPE ? _constants.TEMPLATE_PART_POST_TYPE : _constants.PATTERN_TYPES.user,
        ...rest
      });
    }
    if (path === '/navigation') {
      history.replace({
        postType: _constants.NAVIGATION_POST_TYPE,
        ...rest
      });
    }
  }, [history, params]);
}
function useLayoutAreas() {
  const {
    params
  } = useLocation();
  const {
    postType,
    postId,
    path,
    layout,
    isCustom,
    canvas,
    quickEdit
  } = params;
  const hasEditCanvasMode = canvas === 'edit';
  useRedirectOldPaths();

  // Page list
  if (postType === 'page') {
    const isListLayout = layout === 'list' || !layout;
    const showQuickEdit = quickEdit && !isListLayout;
    return {
      key: 'pages',
      areas: {
        sidebar: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreen.default, {
          title: (0, _i18n.__)('Pages'),
          backPath: {},
          content: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarDataviews.default, {})
        }),
        content: /*#__PURE__*/(0, _jsxRuntime.jsx)(_postList.default, {
          postType: postType
        }),
        preview: !showQuickEdit && (isListLayout || hasEditCanvasMode) && /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}),
        mobile: hasEditCanvasMode ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_postList.default, {
          postType: postType
        }),
        edit: showQuickEdit && /*#__PURE__*/(0, _jsxRuntime.jsx)(_postEdit.PostEdit, {
          postType: postType,
          postId: postId
        })
      },
      widths: {
        content: isListLayout ? 380 : undefined,
        edit: showQuickEdit ? 380 : undefined
      }
    };
  }

  // Templates
  if (postType === _constants.TEMPLATE_POST_TYPE) {
    const isListLayout = isCustom !== 'true' && layout === 'list';
    return {
      key: 'templates',
      areas: {
        sidebar: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreenTemplatesBrowse.default, {
          backPath: {}
        }),
        content: /*#__PURE__*/(0, _jsxRuntime.jsx)(_pageTemplates.default, {}),
        preview: (isListLayout || hasEditCanvasMode) && /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}),
        mobile: hasEditCanvasMode ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_pageTemplates.default, {})
      },
      widths: {
        content: isListLayout ? 380 : undefined
      }
    };
  }

  // Patterns
  if ([_constants.TEMPLATE_PART_POST_TYPE, _constants.PATTERN_TYPES.user].includes(postType)) {
    return {
      key: 'patterns',
      areas: {
        sidebar: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreenPatterns.default, {
          backPath: {}
        }),
        content: /*#__PURE__*/(0, _jsxRuntime.jsx)(_pagePatterns.default, {}),
        mobile: hasEditCanvasMode ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_pagePatterns.default, {}),
        preview: hasEditCanvasMode && /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {})
      }
    };
  }

  // Styles
  if (path === '/wp_global_styles') {
    return {
      key: 'styles',
      areas: {
        sidebar: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreenGlobalStyles.default, {
          backPath: {}
        }),
        preview: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}),
        mobile: hasEditCanvasMode && /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {})
      }
    };
  }

  // Navigation
  if (postType === _constants.NAVIGATION_POST_TYPE) {
    if (postId) {
      return {
        key: 'navigation',
        areas: {
          sidebar: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreenNavigationMenu.default, {
            backPath: {
              postType: _constants.NAVIGATION_POST_TYPE
            }
          }),
          preview: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}),
          mobile: hasEditCanvasMode && /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {})
        }
      };
    }
    return {
      key: 'navigation',
      areas: {
        sidebar: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreenNavigationMenus.default, {
          backPath: {}
        }),
        preview: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}),
        mobile: hasEditCanvasMode && /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {})
      }
    };
  }

  // Fallback shows the home page preview
  return {
    key: 'default',
    areas: {
      sidebar: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationScreenMain.default, {}),
      preview: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}),
      mobile: hasEditCanvasMode && /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {})
    }
  };
}
//# sourceMappingURL=router.js.map