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); }
import React, { useCallback, useMemo, useState } from "react";
import { Platform } from "react-native";
import Box from "../../atoms/box/box";
import Badge from "./badge";
/**
 * A higher-order component which can be used to add a badge to any target component.
 * @param badgeValue The value to be displayed inside the badge
 * @param options A set of options for customizing the look of the badge
 * @returns
 */
const withBadge = (badgeValue = undefined, options = {}) => WrappedComponent => props => {
  // Destructure options with default values
  const {
    placement = "topRight",
    offset = 5,
    hidden = false,
    ...badgeProps
  } = options;

  // State variables to hold the width of the base component and the badge
  const [baseComponentWidth, setBaseComponentWidth] = useState(0);
  const [badgeWidth, setBadgeWidth] = useState(0);

  // Function to compute the position of the badge based on the placement option
  const computePositionForBadge = useCallback(() => {
    const positionValue = -1 * offset;
    if (placement === "topLeft") return {
      top: positionValue,
      left: positionValue
    };
    if (placement === "topRight") return {
      top: positionValue,
      right: positionValue
    };
    if (placement === "bottomLeft") return {
      bottom: positionValue,
      left: positionValue
    };

    // Return bottom right position by default
    return {
      bottom: positionValue,
      right: positionValue
    };
  }, [placement, offset]);

  // Function to update the width of the badge when its layout changes
  const onBadgeLayoutChange = event => {
    const {
      width
    } = event.nativeEvent.layout;
    setBadgeWidth(width);
  };

  // Function to update the width of the base component when its layout changes
  const onBaseComponentLayoutChange = event => {
    const {
      width
    } = event.nativeEvent.layout;
    setBaseComponentWidth(width);
  };

  // Function to compute the position of the badge with margins for web platform
  const computedPositionWithWebMarginsForBadge = useMemo(() => {
    const position = computePositionForBadge();
    if (Platform.OS === "web") {
      return {
        ...position,
        margin: position.right !== undefined ? baseComponentWidth - badgeWidth - position.right : position.left,
        marginVertical: 0,
        marginRight: 0
      };
    }
    return position;
  }, [computePositionForBadge, baseComponentWidth, badgeWidth]);

  // Render the wrapped component with the badge
  return /*#__PURE__*/React.createElement(Box, null, /*#__PURE__*/React.createElement(WrappedComponent, _extends({}, props, {
    onLayout: onBaseComponentLayoutChange
  })), !hidden && /*#__PURE__*/React.createElement(Badge, _extends({}, badgeProps, {
    onLayout: onBadgeLayoutChange,
    position: "absolute",
    zIndex: "overlay",
    style: computedPositionWithWebMarginsForBadge
  }), badgeValue));
};
export default withBadge;
//# sourceMappingURL=withBadge.js.map