"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _box = _interopRequireDefault(require("../../atoms/box/box"));
var _badge = _interopRequireDefault(require("./badge"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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); }
/**
 * 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] = (0, _react.useState)(0);
  const [badgeWidth, setBadgeWidth] = (0, _react.useState)(0);

  // Function to compute the position of the badge based on the placement option
  const computePositionForBadge = (0, _react.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 = (0, _react.useMemo)(() => {
    const position = computePositionForBadge();
    if (_reactNative.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.default.createElement(_box.default, null, /*#__PURE__*/_react.default.createElement(WrappedComponent, _extends({}, props, {
    onLayout: onBaseComponentLayoutChange
  })), !hidden && /*#__PURE__*/_react.default.createElement(_badge.default, _extends({}, badgeProps, {
    onLayout: onBadgeLayoutChange,
    position: "absolute",
    zIndex: "overlay",
    style: computedPositionWithWebMarginsForBadge
  }), badgeValue));
};
var _default = exports.default = withBadge;
//# sourceMappingURL=withBadge.js.map