"use strict";

import { createRef, useEffect, useLayoutEffect } from "react";
import { AccessibilityInfo, findNodeHandle, StyleSheet, View } from "react-native";
import Animated, { useAnimatedStyle, useScrollOffset, useSharedValue, withTiming } from "react-native-reanimated";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useIOTheme } from "../../context";
import { alertEdgeToEdgeInsetTransitionConfig, IOColors, IOVisualCostants } from "../../core";
import { IconButton } from "../buttons";
import { HStack } from "../layout";
import { H2 } from "../typography";

// Three actions
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const styles = StyleSheet.create({
  headerInner: {
    paddingHorizontal: IOVisualCostants.appMarginDefault,
    height: IOVisualCostants.headerHeight,
    width: "100%",
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "space-between"
  },
  headerDivider: {
    position: "absolute",
    width: "100%",
    height: StyleSheet.hairlineWidth,
    left: 0,
    right: 0,
    bottom: 0
  }
});
export const HeaderFirstLevel = ({
  title,
  testID,
  actions = [],
  ignoreSafeAreaMargin = false,
  animatedRef,
  variant = "primary",
  animatedFlatListRef
}) => {
  const titleRef = /*#__PURE__*/createRef();
  const insets = useSafeAreaInsets();
  const theme = useIOTheme();
  const paddingTop = useSharedValue(ignoreSafeAreaMargin ? 0 : insets.top);
  const isPrimary = variant === "primary";
  useLayoutEffect(() => {
    const reactNode = findNodeHandle(titleRef.current);
    if (reactNode !== null) {
      AccessibilityInfo.setAccessibilityFocus(reactNode);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  /* We show the divider only when the header is scrolled down */
  const offset = useScrollOffset(animatedRef || animatedFlatListRef);
  useEffect(() => {
    // eslint-disable-next-line functional/immutable-data
    paddingTop.value = withTiming(ignoreSafeAreaMargin ? 0 : insets.top, alertEdgeToEdgeInsetTransitionConfig);
  }, [ignoreSafeAreaMargin, insets.top, paddingTop]);
  const animatedStyle = useAnimatedStyle(() => ({
    paddingTop: paddingTop.value
  }));
  const animatedDivider = useAnimatedStyle(() => ({
    opacity: withTiming(offset.value > 0 ? 1 : 0, {
      duration: 200
    })
  }));
  return /*#__PURE__*/_jsxs(Animated.View, {
    style: [{
      backgroundColor: isPrimary ? IOColors[theme["appBackground-primary"]] : IOColors[theme["appBackground-accent"]]
    }, animatedStyle],
    accessibilityRole: "header",
    testID: testID,
    children: [(animatedRef || animatedFlatListRef) && isPrimary && /*#__PURE__*/_jsx(Animated.View, {
      style: [{
        ...styles.headerDivider,
        backgroundColor: IOColors[theme["divider-default"]]
      }, animatedDivider]
    }), /*#__PURE__*/_jsxs(View, {
      style: styles.headerInner,
      children: [/*#__PURE__*/_jsx(View, {
        ref: titleRef,
        accessible: true,
        accessibilityRole: "header",
        children: /*#__PURE__*/_jsx(H2, {
          weight: "Bold",
          style: {
            flexShrink: 1
          },
          numberOfLines: 1,
          color: isPrimary ? theme["textHeading-default"] : theme["textHeading-constrast"],
          maxFontSizeMultiplier: 1.25,
          children: title
        })
      }), /*#__PURE__*/_jsx(HStack, {
        space: 16,
        style: {
          flexShrink: 0
        },
        children: actions.map(action => /*#__PURE__*/_jsx(IconButton, {
          ...action,
          color: variant
        }, `header-first-level-action-${action.icon}`))
      })]
    })]
  });
};
//# sourceMappingURL=HeaderFirstLevel.js.map