import React, { useContext } from 'react';
import classNames from 'classnames';
import ConfigProvider from '../config-provider';
import Affix from '../affix';
import Tabs from '../tabs';
import PageHeader from '../page-header';
import RouteContext from '../wau-route-context';
import GridContent from '../wau-grid-content';
import FooterToolbar from '../wau-footer-toolbar';
import PageLoading from '../wau-page-loading';
// import type { WaterMarkProps } from './water-mark';
// import WaterMark from './water-mark';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
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__*/_jsx(Tabs, {
      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__*/_jsx("div", {
    className: `${prefixedClassName}-detail`,
    children: /*#__PURE__*/_jsx("div", {
      className: `${prefixedClassName}-main`,
      children: /*#__PURE__*/_jsxs("div", {
        className: `${prefixedClassName}-row`,
        children: [content && /*#__PURE__*/_jsx("div", {
          className: `${prefixedClassName}-content`,
          children: content
        }), extraContent && /*#__PURE__*/_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__*/_jsx(PageHeader, {
    ...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 = useContext(RouteContext);
  const {
    getPrefixCls
  } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = props.prefixCls || getPrefixCls('pro');
  const prefixedClassName = `${prefixCls}-page-container`;
  const className = classNames(prefixedClassName, propsClassName, {
    [`${prefixCls}-page-container-ghost`]: ghost
  });
  const content = children ? /*#__PURE__*/_jsxs("div", {
    children: [/*#__PURE__*/_jsx("div", {
      className: `${prefixedClassName}-children-content`,
      children: children
    }), value.hasFooterToolbar && /*#__PURE__*/_jsx("div", {
      style: {
        height: 48,
        marginTop: 24
      }
    })]
  }) : null;
  const pageHeaderDom = defaultPageHeaderRender(props, {
    ...value,
    prefixCls: undefined,
    prefixedClassName
  });
  const headerDom = pageHeaderDom ? /*#__PURE__*/_jsx("div", {
    className: `${prefixedClassName}-warp`,
    children: pageHeaderDom
  }) : null;
  const renderContent = () => {
    const spinProps = genLoading(loading);
    const dom = spinProps.spinning ? /*#__PURE__*/_jsx(PageLoading, {
      ...spinProps
    }) : content;
    // if (props.waterMarkProps || value.waterMarkProps) {
    //   return (
    //     <WaterMark {...(props.waterMarkProps || value.waterMarkProps)}>
    //       {dom}
    //     </WaterMark>
    //   );
    // }
    return dom;
  };
  return /*#__PURE__*/_jsxs("div", {
    style: style,
    className: className,
    children: [fixedHeader && headerDom ?
    /*#__PURE__*/
    // 在 hasHeader 且 fixedHeader 的情况下，才需要设置高度
    _jsx(Affix, {
      offsetTop: value.hasHeader && value.fixedHeader ? value.headerHeight : 0,
      ...affixProps,
      children: headerDom
    }) : headerDom, /*#__PURE__*/_jsx(GridContent, {
      children: renderContent()
    }), footer && /*#__PURE__*/_jsx(FooterToolbar, {
      prefixCls: prefixCls,
      children: footer
    })]
  });
};
PageContainer.defaultProps = {
  fixedHeader: true
};
export default PageContainer;