import { forwardRef } from 'react';

export interface PolymorphicWithOverridesProps {
  __overrides: {
    as: React.ElementType;
    [key: string]: unknown;
  };
  [key: string]: unknown;
}

/**
 * For overcoming the limitations of third-party components with an `as` prop.
 */
export const PolymorphicWithOverrides = forwardRef<Element | null, PolymorphicWithOverridesProps>(
  function PolymorphicWithOverrides(
    { __overrides, ...restProps },
    ref: React.ForwardedRef<Element | null>,
  ) {
    const { as: Element, ...restOverrides } =
      __overrides as PolymorphicWithOverridesProps['__overrides'];
    return <Element ref={ref} {...restProps} {...restOverrides} />;
  },
);
