"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _affix = _interopRequireDefault(require("../affix"));
var _tabs = _interopRequireDefault(require("../tabs"));
var _pageHeader = _interopRequireDefault(require("../page-header"));
var _wauRouteContext = _interopRequireDefault(require("../wau-route-context"));
var _wauGridContent = _interopRequireDefault(require("../wau-grid-content"));
var _wauFooterToolbar = _interopRequireDefault(require("../wau-footer-toolbar"));
var _wauPageLoading = _interopRequireDefault(require("../wau-page-loading"));
require("./style");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// import type { WaterMarkProps } from './water-mark';
// import WaterMark from './water-mark';
function genLoading(spinProps) {
  if (typeof spinProps === 'object') {
    return spinProps;
  }
  return {
    spinning: spinProps
  };
}

/**
 * Render Footer tabList In order to be compatible with the old version of the PageHeader basically
 * all the functions are implemented.
 */
const renderFooter = ({
  tabList,
  tabActiveKey,
  onTabChange,
  tabBarExtraContent,
  tabProps,
  prefixedClassName,
  tabItems
}) => {
  const items = (() => {
    if (tabItems) {
      return tabItems;
    } else if (tabList) {
      return tabList.map(tab => {
        var _tab$key;
        return {
          ...tab,
          label: tab.tab,
          key: (_tab$key = tab.key) !== null && _tab$key !== void 0 ? _tab$key : tab.tabKey
        };
      });
    }
    return [];
  })();
  if (items && items.length) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_tabs.default, {
      className: `${prefixedClassName}-tabs`,
      activeKey: tabActiveKey,
      onChange: key => {
        if (onTabChange) {
          onTabChange(key);
        }
      },
      tabBarExtraContent: tabBarExtraContent,
      items: items,
      ...tabProps
    });
  }
  return null;
};
const renderPageHeader = (content, extraContent, prefixedClassName) => {
  if (!content && !extraContent) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: `${prefixedClassName}-detail`,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: `${prefixedClassName}-main`,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: `${prefixedClassName}-row`,
        children: [content && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: `${prefixedClassName}-content`,
          children: content
        }), extraContent && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: `${prefixedClassName}-extraContent`,
          children: extraContent
        })]
      })
    })
  });
};
const defaultPageHeaderRender = (props, value) => {
  var _breadcrumb$routes;
  const {
    title,
    content,
    pageHeaderRender,
    header,
    extraContent,
    prefixCls,
    ...restProps
  } = props;
  if (pageHeaderRender === false) {
    return null;
  }
  if (pageHeaderRender) {
    return pageHeaderRender({
      ...props,
      ...value
    });
  }
  let pageHeaderTitle = title;
  if (!title && title !== false) {
    pageHeaderTitle = value.title;
  }
  const pageHeaderProps = {
    ...value,
    title: pageHeaderTitle,
    ...restProps,
    footer: renderFooter({
      ...restProps,
      prefixedClassName: value.prefixedClassName
    }),
    className: header === null || header === void 0 ? void 0 : header.className,
    ...header
  };
  const {
    breadcrumb
  } = pageHeaderProps;
  const pageHeaderPropsKeys = ['title', 'subTitle', 'breadcrumb', 'extra', 'tags', 'footer', 'avatar', 'backIcon'];
  if (pageHeaderPropsKeys.every(item => !pageHeaderProps[item]) && (!breadcrumb || !(breadcrumb !== null && breadcrumb !== void 0 && breadcrumb.itemRender) && !(breadcrumb !== null && breadcrumb !== void 0 && (_breadcrumb$routes = breadcrumb.routes) !== null && _breadcrumb$routes !== void 0 && _breadcrumb$routes.length)) && !content && !extraContent) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_pageHeader.default, {
    ...pageHeaderProps,
    breadcrumb: {
      ...pageHeaderProps.breadcrumb,
      ...value.breadcrumbProps
    },
    prefixCls: prefixCls,
    children: (header === null || header === void 0 ? void 0 : header.children) || renderPageHeader(content, extraContent, value.prefixedClassName)
  });
};
const PageContainer = props => {
  const {
    children,
    loading = false,
    style,
    footer,
    affixProps,
    ghost,
    fixedHeader,
    className: propsClassName
  } = props;
  const value = (0, _react.useContext)(_wauRouteContext.default);
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = props.prefixCls || getPrefixCls('pro');
  const prefixedClassName = `${prefixCls}-page-container`;
  const className = (0, _classnames.default)(prefixedClassName, propsClassName, {
    [`${prefixCls}-page-container-ghost`]: ghost
  });
  const content = children ? /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: `${prefixedClassName}-children-content`,
      children: children
    }), value.hasFooterToolbar && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      style: {
        height: 48,
        marginTop: 24
      }
    })]
  }) : null;
  const pageHeaderDom = defaultPageHeaderRender(props, {
    ...value,
    prefixCls: undefined,
    prefixedClassName
  });
  const headerDom = pageHeaderDom ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: `${prefixedClassName}-warp`,
    children: pageHeaderDom
  }) : null;
  const renderContent = () => {
    const spinProps = genLoading(loading);
    const dom = spinProps.spinning ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauPageLoading.default, {
      ...spinProps
    }) : content;
    // if (props.waterMarkProps || value.waterMarkProps) {
    //   return (
    //     <WaterMark {...(props.waterMarkProps || value.waterMarkProps)}>
    //       {dom}
    //     </WaterMark>
    //   );
    // }
    return dom;
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    style: style,
    className: className,
    children: [fixedHeader && headerDom ?
    /*#__PURE__*/
    // 在 hasHeader 且 fixedHeader 的情况下，才需要设置高度
    (0, _jsxRuntime.jsx)(_affix.default, {
      offsetTop: value.hasHeader && value.fixedHeader ? value.headerHeight : 0,
      ...affixProps,
      children: headerDom
    }) : headerDom, /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauGridContent.default, {
      children: renderContent()
    }), footer && /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauFooterToolbar.default, {
      prefixCls: prefixCls,
      children: footer
    })]
  });
};
PageContainer.defaultProps = {
  fixedHeader: true
};
var _default = PageContainer;
exports.default = _default;