"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Header = exports.DEFAULT_APPBAR_HEIGHT = void 0;
var React = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _color = _interopRequireDefault(require("color"));
var _Surface = _interopRequireDefault(require("../Surface"));
var _HeaderAction = _interopRequireDefault(require("./HeaderAction"));
var _HeaderBackAction = _interopRequireDefault(require("./HeaderBackAction"));
var _HeaderContent = _interopRequireDefault(require("./HeaderContent"));
var _theming = require("../../core/theming");
var _tokens = require("../../styles/tokens");
var _Searchbar = _interopRequireDefault(require("../Searchbar"));
var _HeaderContext = require("./HeaderContext");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const DEFAULT_APPBAR_HEIGHT = 56;
exports.DEFAULT_APPBAR_HEIGHT = DEFAULT_APPBAR_HEIGHT;
const Header = _ref => {
  let {
    children,
    dark,
    style,
    theme,
    elevated,
    safeAreaInsets,
    align,
    ...rest
  } = _ref;
  const {
    options,
    setOptions
  } = (0, _HeaderContext.useHeader)();
  const {
    backgroundColor: customBackground,
    ...restStyle
  } = _reactNative.StyleSheet.flatten(style) || {};
  const {
    dark: isDarkTheme
  } = theme;
  let isDark;
  const backgroundColor = customBackground ? customBackground : "transparent";
  if (typeof dark === "boolean") {
    isDark = dark;
  } else {
    isDark = backgroundColor === "transparent" ? false : typeof backgroundColor === "string" ? !(0, _color.default)(backgroundColor).isLight() : true;
  }
  let shouldCenterContent = false;
  let shouldAddLeftSpacing = false;
  let shouldAddRightSpacing = false;
  if (_reactNative.Platform.OS === "ios" || align === "center") {
    let hasAppbarContent = false;
    let leftItemsCount = 0;
    let rightItemsCount = 0;
    React.Children.forEach(children, child => {
      if ( /*#__PURE__*/React.isValidElement(child)) {
        if (child.type === _HeaderContent.default) {
          hasAppbarContent = true;
        } else if (hasAppbarContent) {
          rightItemsCount++;
        } else {
          leftItemsCount++;
        }
      }
    });
    shouldCenterContent = hasAppbarContent && leftItemsCount < 2 && rightItemsCount < 2;
    shouldAddLeftSpacing = shouldCenterContent && leftItemsCount === 0;
    shouldAddRightSpacing = shouldCenterContent && rightItemsCount === 0;
  }
  const renderAppbarContent = _ref2 => {
    let {
      children,
      isDark,
      shouldCenterContent = false,
      renderOnly,
      renderExcept
    } = _ref2;
    const content = React.Children.toArray(children).filter(child => child != null && typeof child !== "boolean").filter(child =>
    // @ts-expect-error: TypeScript complains about the type of type but it doesn't matter
    renderExcept ? !renderExcept.includes(child.type) : child).filter((child // @ts-expect-error: TypeScript complains about the type of type but it doesn't matter
    ) => renderOnly ? renderOnly.includes(child.type) : child).map((child, i) => {
      if (! /*#__PURE__*/React.isValidElement(child) || ![_HeaderContent.default, _HeaderAction.default, _HeaderBackAction.default].includes(
      // @ts-expect-error: TypeScript complains about the type of type but it doesn't matter
      child.type)) {
        return child;
      }
      const props = {
        color: typeof child.props.color !== "undefined" ? child.props.color : isDark ? _tokens.colors.white : _tokens.colors.black
      };
      if (child.type === _HeaderContent.default) {
        props.style = [i !== 0 && {
          marginLeft: 8
        },, shouldCenterContent && styles.centerAlignedContent, child.props.style];
      }
      return /*#__PURE__*/React.cloneElement(child, props);
    });
    return content;
  };
  const spacingStyle = styles.spacing;
  const insets = {
    paddingBottom: safeAreaInsets === null || safeAreaInsets === void 0 ? void 0 : safeAreaInsets.bottom,
    paddingTop: safeAreaInsets === null || safeAreaInsets === void 0 ? void 0 : safeAreaInsets.top,
    paddingLeft: safeAreaInsets === null || safeAreaInsets === void 0 ? void 0 : safeAreaInsets.left,
    paddingRight: safeAreaInsets === null || safeAreaInsets === void 0 ? void 0 : safeAreaInsets.right
  };
  const headerTranslateY = options.scrollY.interpolate({
    inputRange: [0, 56],
    outputRange: [0, -56],
    extrapolate: "clamp"
  });
  const animatedSearchbarStyle = {
    ...styles.animatedSearchBarWrapper,
    transform: [{
      scaleY: options.scrollY.interpolate({
        inputRange: [0, 56],
        outputRange: [1, 0],
        extrapolate: "clamp"
      })
    }
    // { translateY: headerTranslateY },
    ]
  };

  const searchbarStyle = options.animate && _reactNative.Platform.OS !== "web" ? animatedSearchbarStyle : styles.searchBarWrapper;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_Surface.default, _extends({
    style: [{
      backgroundColor
    }, styles.appbar, {
      height: DEFAULT_APPBAR_HEIGHT
    }, insets, restStyle]
  }, rest), shouldAddLeftSpacing ? /*#__PURE__*/React.createElement(_reactNative.View, {
    style: spacingStyle
  }) : null, renderAppbarContent({
    children,
    isDark,
    shouldCenterContent: align === "center" || shouldCenterContent
  }), shouldAddRightSpacing ? /*#__PURE__*/React.createElement(_reactNative.View, {
    style: spacingStyle
  }) : null), /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
    style: [searchbarStyle],
    pointerEvents: "box-none"
  }, /*#__PURE__*/React.createElement(_Searchbar.default, _extends({
    placeholder: "Search"
  }, rest.searchbarOptions, {
    style: {
      marginBottom: 8
    }
  }))));
};
const styles = _reactNative.StyleSheet.create({
  appbar: {
    flexDirection: "row",
    alignItems: "center",
    paddingHorizontal: 4,
    width: "100%",
    marginVertical: 8
  },
  centerAlignedContent: {
    alignItems: "center"
  },
  spacing: {
    width: 48
  },
  v3Spacing: {
    width: 52
  },
  controlsRow: {
    flex: 1,
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "space-between"
  },
  rightActionControls: {
    flexDirection: "row",
    flex: 1,
    justifyContent: "flex-end"
  },
  columnContainer: {
    flexDirection: "column",
    flex: 1,
    paddingTop: 8
  },
  centerAlignedContainer: {
    paddingTop: 0
  },
  animatedSearchBarWrapper: {
    position: "absolute",
    top: 0,
    left: 0,
    right: 0,
    zIndex: 1,
    paddingHorizontal: 8,
    height: 56 + 40 + 24,
    justifyContent: "flex-end",
    pointerEvents: "none"
  },
  searchBarWrapper: {
    width: "100%",
    paddingHorizontal: 8
  }
});
var _default = (0, _theming.withTheme)(Header); // @component-docs ignore-next-line
exports.default = _default;
const HeaderWithTheme = (0, _theming.withTheme)(Header);
// @component-docs ignore-next-line
exports.Header = HeaderWithTheme;
//# sourceMappingURL=Header.js.map