"use strict";

import React, { useCallback, useContext, useEffect } from 'react';
import { BackHandler, DeviceEventEmitter, StyleSheet, TouchableOpacity, View } from 'react-native';
import { Colors, Opacity } from "../../Consts/index.js";
import { ApplicationContext } from "../../Context/index.js";
import { Icon } from "../../Icon/index.js";
import { Text } from "../../Text/index.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const NavigationButton = ({
  icon,
  tintColor,
  onPress,
  style,
  badge
}) => {
  const {
    theme
  } = useContext(ApplicationContext);
  let buttonStyle = {
    backgroundColor: theme.colors.background.surface + Opacity['60'],
    borderColor: theme.colors.border.default,
    borderWidth: 0.5
  };
  if (tintColor === Colors.white) {
    buttonStyle = {
      borderColor: theme.colors.border.default,
      backgroundColor: theme.colors.text.default + Opacity['60']
    };
  }
  const buildBadge = () => {
    if (badge) {
      if (badge === 'dot') {
        return /*#__PURE__*/_jsx(View, {
          style: [styles.badgeDot, {
            backgroundColor: theme.colors.error.default
          }]
        });
      }
      return /*#__PURE__*/_jsx(View, {
        style: [styles.badgeLabel, {
          backgroundColor: theme.colors.error.default
        }],
        children: /*#__PURE__*/_jsx(Text, {
          typography: 'caption2',
          color: Colors.white,
          children: badge
        })
      });
    }
    return null;
  };
  return /*#__PURE__*/_jsxs(TouchableOpacity, {
    style: [styles.navigationButton, style, buttonStyle],
    onPress: onPress,
    children: [/*#__PURE__*/_jsx(Icon, {
      name: icon,
      color: tintColor ?? theme.colors.text.default,
      size: 20
    }), buildBadge()]
  });
};
const NavigationBackButton = /*#__PURE__*/React.memo(({
  onPress,
  ...props
}) => {
  const {
    navigator
  } = useContext(ApplicationContext);
  const goBack = useCallback(() => {
    const canGoBack = navigator?.ref.current?.canGoBack();
    if (canGoBack) {
      navigator?.ref.current?.goBack();
    } else {
      DeviceEventEmitter.emit('dismiss', undefined);
    }
    onPress?.();
    return true;
  }, [navigator?.ref, onPress]);
  useEffect(() => {
    const backHandler = BackHandler.addEventListener('hardwareBackPress', goBack);
    return () => backHandler.remove();
  }, [goBack]);
  return /*#__PURE__*/_jsx(View, {
    style: styles.headerLeft,
    children: /*#__PURE__*/_jsx(NavigationButton, {
      ...props,
      onPress: goBack
    })
  });
});
const HeaderRightAction = ({
  children,
  ...restProps
}) => {
  const renderAction = () => {
    if (Array.isArray(children)) {
      return children.map((child, index) => {
        return /*#__PURE__*/_jsx(View, {
          style: styles.headerButton,
          children: /*#__PURE__*/React.cloneElement(child, {
            ...restProps
          })
        }, index);
      });
    }
    return /*#__PURE__*/_jsx(View, {
      style: styles.headerButton,
      children: /*#__PURE__*/React.cloneElement(children, {
        ...restProps
      })
    });
  };
  return /*#__PURE__*/_jsx(View, {
    style: styles.headerRightButton,
    children: renderAction()
  });
};
const styles = StyleSheet.create({
  headerButton: {
    paddingHorizontal: 4
  },
  headerRightButton: {
    flexDirection: 'row',
    overflow: 'hidden',
    justifyContent: 'center',
    alignItems: 'center',
    marginRight: 12
  },
  navigationButton: {
    height: 28,
    width: 28,
    borderRadius: 14,
    borderWidth: 0.2,
    justifyContent: 'center',
    alignItems: 'center'
  },
  headerLeft: {
    marginLeft: 12
  },
  badgeDot: {
    position: 'absolute',
    top: -2,
    right: 0,
    height: 8,
    width: 8,
    borderRadius: 4
  },
  badgeLabel: {
    position: 'absolute',
    top: -4,
    right: -4,
    color: 'white',
    borderRadius: 7,
    height: 14,
    minWidth: 14,
    paddingHorizontal: 2,
    alignItems: 'center',
    justifyContent: 'center'
  }
});
export { NavigationButton, NavigationBackButton, HeaderRightAction };
//# sourceMappingURL=NavigationButton.js.map