var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
import { mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
import { isNeedOpenHash, ProProvider } from "@antdv/pro-provider";
import { boolType, classNames, numberType, omit, oneOfType, useState, vNodeType } from "@antdv/pro-utils";
import { ConfigProvider, Layout } from "ant-design-vue";
import { useConfigContextInject } from "ant-design-vue/lib/config-provider/context";
import { computed, defineComponent, onMounted, onUnmounted } from "vue";
import { clearMenuItem } from "../../utils/utils.mjs";
import { GlobalHeader } from "../GlobalHeader/index.mjs";
import { globalHeaderProps } from "../GlobalHeader/props.mjs";
import { privateSiderMenuProps } from "../SiderMenu/SiderMenu.mjs";
import { TopNavHeader } from "../TopNavHeader/index.mjs";
import { useStyle } from "./style/header.mjs";
import { useStylish } from "./style/stylish.mjs";
const {
  Header
} = Layout;
function headerViewProps() {
  return __spreadProps(__spreadValues({}, globalHeaderProps()), {
    isMobile: boolType(),
    logo: vNodeType(),
    headerRender: oneOfType([Function, Boolean]).def(void 0),
    headerTitleRender: oneOfType([Function, Boolean]).def(void 0),
    headerContentRender: oneOfType([Function, Boolean]).def(void 0),
    siderWidth: numberType(),
    hasSiderMenu: boolType()
  });
}
const DefaultHeader = defineComponent({
  name: "DefaultHeader",
  inheritAttrs: false,
  props: __spreadValues(__spreadValues({}, headerViewProps()), privateSiderMenuProps()),
  slots: Object,
  setup(props, {
    attrs
  }) {
    const {
      token
    } = ProProvider.useContext();
    const {
      getTargetContainer
    } = useConfigContextInject();
    const needFixedHeader = computed(() => props.fixedHeader || props.layout === "mix");
    const dom = computed(() => {
      var _a;
      return ((_a = getTargetContainer == null ? void 0 : getTargetContainer.value) == null ? void 0 : _a.call(getTargetContainer)) || document.body;
    });
    const [isFixedHeaderScroll, setIsFixedHeaderScroll] = useState(false);
    const isFixedHeaderFn = () => {
      var _a, _b;
      const scrollTop = dom.value.scrollTop;
      if (scrollTop > (((_b = (_a = token == null ? void 0 : token.layout) == null ? void 0 : _a.header) == null ? void 0 : _b.heightLayoutHeader) || 56) && !isFixedHeaderScroll.value) {
        setIsFixedHeaderScroll(true);
        return true;
      }
      if (isFixedHeaderScroll.value) {
        setIsFixedHeaderScroll(false);
      }
      return false;
    };
    const baseClassName = computed(() => `${props.prefixCls}-layout-header`);
    const {
      wrapSSR,
      hashId
    } = useStyle(baseClassName);
    const stylish = useStylish(computed(() => `${baseClassName.value}.${baseClassName.value}-stylish`), {
      proLayoutCollapsedWidth: 64,
      stylish: props.stylish
    });
    onMounted(() => {
      var _a;
      if (!needFixedHeader.value) return;
      if (typeof window === "undefined") return;
      (_a = dom.value) == null ? void 0 : _a.addEventListener("scroll", isFixedHeaderFn, {
        passive: true
      });
    });
    onUnmounted(() => {
      var _a;
      (_a = dom.value) == null ? void 0 : _a.removeEventListener("scroll", isFixedHeaderFn);
    });
    return () => {
      const {
        isMobile,
        collapsed,
        onCollapse,
        layout,
        headerRender,
        headerContentRender
      } = props;
      const mergeProps = __spreadValues(__spreadValues({}, omit(props, ["onCollapse"])), attrs);
      const renderContent = () => {
        const isTop = layout === "top";
        const clearMenuData = clearMenuItem(props.menuData || []);
        let defaultDom = _createVNode(GlobalHeader, _mergeProps({
          "onCollapse": onCollapse
        }, mergeProps, {
          "menuData": clearMenuData
        }), {
          default: () => [headerContentRender && headerContentRender()]
        });
        if (isTop && !isMobile) {
          defaultDom = _createVNode(TopNavHeader, _mergeProps({
            "mode": "horizontal",
            "onCollapse": onCollapse
          }, mergeProps, {
            "menuData": clearMenuData
          }), null);
        }
        if (headerRender && typeof headerRender === "function") {
          return headerRender(props, defaultDom);
        }
        return defaultDom;
      };
      const className = classNames(attrs.class || "", hashId.value, baseClassName.value, {
        [`${baseClassName.value}-fixed-header`]: needFixedHeader.value,
        [`${baseClassName.value}-fixed-header-scroll`]: isFixedHeaderScroll.value,
        [`${baseClassName.value}-mix`]: layout === "mix",
        [`${baseClassName.value}-fixed-header-action`]: !collapsed,
        [`${baseClassName.value}-top-menu`]: layout === "top",
        [`${baseClassName.value}-header`]: true,
        [`${baseClassName.value}-stylish`]: !!props.stylish
      });
      if (layout === "side" && !isMobile) return null;
      return stylish.wrapSSR(wrapSSR(_createVNode(ConfigProvider, {
        "theme": {
          hashed: isNeedOpenHash(),
          components: {
            Layout: {
              // @ts-expect-error
              headerBg: "transparent",
              bodyBg: "transparent"
            }
          }
        }
      }, {
        default: () => {
          var _a, _b, _c, _d;
          return [needFixedHeader.value && _createVNode(Header, {
            "style": __spreadValues({
              height: `${((_b = (_a = token == null ? void 0 : token.layout) == null ? void 0 : _a.header) == null ? void 0 : _b.heightLayoutHeader) || 56}px`,
              lineHeight: `${((_d = (_c = token == null ? void 0 : token.layout) == null ? void 0 : _c.header) == null ? void 0 : _d.heightLayoutHeader) || 56}px`,
              backgroundColor: "transparent",
              zIndex: 19
            }, attrs.style || {})
          }, null), _createVNode(Header, {
            "class": className,
            "style": attrs.style
          }, {
            default: () => [renderContent()]
          })];
        }
      })));
    };
  }
});
export {
  DefaultHeader,
  headerViewProps
};
