"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.HeaderFirstLevel = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated"));
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _context = require("../../context");
var _core = require("../../core");
var _buttons = require("../buttons");
var _layout = require("../layout");
var _typography = require("../typography");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
// Three actions

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

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