import { describe, it } from 'vitest';
import type { ForwardedRef } from 'react';
import { forwardRef } from '@wordpress/element';
import type { WordPressComponentProps } from '../wordpress-component';
import { contextConnect, contextConnectWithoutRef } from '../context-connect';

// Static TypeScript checks
/* eslint-disable vitest/expect-expect -- TypeScript checks these cases during typecheck. */
describe( 'WordPressComponentProps', () => {
	it( 'should not accept a ref', () => {
		const Foo = ( props: WordPressComponentProps< {}, 'div' > ) => (
			<div { ...props } />
		);

		// @ts-expect-error The ref prop should trigger an error.
		<Foo ref={ null } />;
	} );

	it( 'should accept a ref if wrapped by a forwardRef()', () => {
		const Foo = (
			props: WordPressComponentProps< {}, 'div' >,
			ref: ForwardedRef< any >
		) => <div { ...props } ref={ ref } />;
		const ForwardedFoo = forwardRef( Foo );

		<ForwardedFoo ref={ null } />;
	} );
} );

describe( 'WordPressComponentFromProps', () => {
	it( 'should preserve as for polymorphic connected components', () => {
		const Unconnected = (
			props: WordPressComponentProps< {}, 'div' >,
			ref: ForwardedRef< any >
		) => <div { ...props } ref={ ref } />;
		const Connected = contextConnect( Unconnected, 'Polymorphic' );

		<Connected as="label" htmlFor="field" />;
	} );

	it( 'should reject as on a non-polymorphic connected component', () => {
		const Unconnected = (
			props: WordPressComponentProps< {}, null, false >
		) => <div { ...props } />;
		const Foo = contextConnectWithoutRef( Unconnected, 'Foo' );

		// @ts-expect-error Non-polymorphic components must not accept `as`.
		<Foo as="div" />;
	} );
} );
/* eslint-enable vitest/expect-expect */
