import {
	areGlobalStylesEqual,
	privateApis as globalStylesEnginePrivateApis,
} from '@wordpress/global-styles-engine';
import type {
	GlobalStylesConfig,
	GlobalStylesSettings,
} from '@wordpress/global-styles-engine';
import { __ } from '@wordpress/i18n';
import { unlock } from './lock-unlock';

const { getViewportBreakpoints, resolveStyle } = unlock(
	globalStylesEnginePrivateApis
);

/**
 * State definition with value and label.
 */
export interface StateDefinition {
	value: string;
	label: string;
}

/**
 * Valid states for elements with their labels.
 * This mirrors the PHP constant in lib/class-wp-theme-json-gutenberg.php
 */
export const VALID_ELEMENT_STATES: Record< string, StateDefinition[] > = {
	link: [
		{ value: ':link', label: __( 'Link' ) },
		{ value: ':any-link', label: __( 'Any Link' ) },
		{ value: ':visited', label: __( 'Visited' ) },
		{ value: ':hover', label: __( 'Hover' ) },
		{ value: ':focus', label: __( 'Focus' ) },
		{ value: ':focus-visible', label: __( 'Focus-visible' ) },
		{ value: ':active', label: __( 'Active' ) },
	],
	button: [
		{ value: ':link', label: __( 'Link' ) },
		{ value: ':any-link', label: __( 'Any Link' ) },
		{ value: ':visited', label: __( 'Visited' ) },
		{ value: ':hover', label: __( 'Hover' ) },
		{ value: ':focus', label: __( 'Focus' ) },
		{ value: ':focus-visible', label: __( 'Focus-visible' ) },
		{ value: ':active', label: __( 'Active' ) },
	],
};

/**
 * Valid states for blocks with their labels.
 * This mirrors the PHP constant in lib/class-wp-theme-json-gutenberg.php
 */
export const VALID_BLOCK_STATES: Record< string, StateDefinition[] > = {
	'core/button': [
		{ value: ':hover', label: __( 'Hover' ) },
		{ value: ':focus', label: __( 'Focus' ) },
		{ value: ':focus-visible', label: __( 'Focus-visible' ) },
		{ value: ':active', label: __( 'Active' ) },
	],
	'core/navigation-link': [
		{ value: ':hover', label: __( 'Hover' ) },
		{ value: ':focus', label: __( 'Focus' ) },
		{ value: ':focus-visible', label: __( 'Focus-visible' ) },
		{ value: ':active', label: __( 'Active' ) },
	],
};

/**
 * Responsive breakpoint states available for all blocks.
 * These map to CSS media queries wrapping the block's styles.
 */
export const RESPONSIVE_STATES: StateDefinition[] = [
	{ value: '@tablet', label: __( 'Tablet' ) },
	{ value: '@mobile', label: __( 'Mobile' ) },
];

/**
 * Get the valid pseudo states for a given block or element.
 *
 * @param name The block name (e.g., 'core/button') or element name (e.g., 'button')
 * @return Array of valid pseudo state definitions, or empty array if none
 */
export function getValidPseudoStates( name: string ): StateDefinition[] {
	// Check if it's a block (contains a slash, e.g. 'core/button').
	if ( VALID_BLOCK_STATES[ name ] ) {
		return VALID_BLOCK_STATES[ name ] ?? [];
	}

	// Check if it's an element
	if ( VALID_ELEMENT_STATES[ name ] ) {
		return VALID_ELEMENT_STATES[ name ];
	}

	return [];
}

/**
 * Get the valid viewport state definitions.
 *
 * @param viewportSettings
 * @return Array of valid viewport state definitions.
 */
export function getValidViewportStates(
	viewportSettings?: GlobalStylesSettings[ 'viewport' ]
): StateDefinition[] {
	const breakpoints = getViewportBreakpoints( viewportSettings );

	return RESPONSIVE_STATES.filter(
		( state ) =>
			( state.value !== '@tablet' || breakpoints.tablet !== undefined ) &&
			( state.value !== '@mobile' || breakpoints.mobile !== undefined )
	);
}

/**
 * Removes all instances of properties from an object.
 *
 * @param object     The object to remove the properties from.
 * @param properties The properties to remove.
 * @return The modified object.
 */
export function removePropertiesFromObject(
	object: any,
	properties: string[]
): any {
	if ( ! properties?.length ) {
		return object;
	}

	if (
		typeof object !== 'object' ||
		! object ||
		! Object.keys( object ).length
	) {
		return object;
	}

	for ( const key in object ) {
		if ( properties.includes( key ) ) {
			delete object[ key ];
		} else if ( typeof object[ key ] === 'object' ) {
			removePropertiesFromObject( object[ key ], properties );
		}
	}
	return object;
}

/**
 * Returns a new object, with properties specified in `properties` array.,
 * maintain the original object tree structure.
 * The function is recursive, so it will perform a deep search for the given properties.
 * E.g., the function will return `{ a: { b: { c: { test: 1 } } } }` if the properties are  `[ 'test' ]`.
 *
 * @param object     The object to filter
 * @param properties The properties to filter by
 * @return The merged object.
 */
export const filterObjectByProperties = (
	object: any,
	properties: string[]
): any => {
	if ( ! object || ! properties?.length ) {
		return {};
	}

	const newObject: any = {};
	Object.keys( object ).forEach( ( key ) => {
		if ( properties.includes( key ) ) {
			newObject[ key ] = object[ key ];
		} else if ( typeof object[ key ] === 'object' ) {
			const newFilter = filterObjectByProperties(
				object[ key ],
				properties
			);
			if ( Object.keys( newFilter ).length ) {
				newObject[ key ] = newFilter;
			}
		}
	} );
	return newObject;
};

/**
 * Compares a style variation to the same variation filtered by the specified properties.
 * Returns true if the variation contains only the properties specified.
 *
 * @param variation  The variation to compare.
 * @param properties The properties to compare.
 * @return Whether the variation contains only the specified properties.
 */
export function isVariationWithProperties(
	variation: GlobalStylesConfig,
	properties: string[]
): boolean {
	const variationWithProperties = filterObjectByProperties(
		structuredClone( variation ),
		properties
	);

	return areGlobalStylesEqual( variationWithProperties, variation );
}

function getFontFamilyFromSetting( fontFamilies: any[], setting: string ): any {
	if ( ! Array.isArray( fontFamilies ) || ! setting ) {
		return null;
	}

	const fontFamilyVariable = setting.replace( 'var(', '' ).replace( ')', '' );
	const fontFamilySlug = fontFamilyVariable?.split( '--' ).slice( -1 )[ 0 ];

	return fontFamilies.find(
		( fontFamily ) => fontFamily.slug === fontFamilySlug
	);
}

/**
 * Extracts font families from a theme JSON configuration.
 *
 * @param themeJson The theme JSON configuration
 * @return Array containing [bodyFontFamily, headingFontFamily]
 */
export function getFontFamilies( themeJson: any ): [ any, any ] {
	const themeFontFamilies =
		themeJson?.settings?.typography?.fontFamilies?.theme;
	const customFontFamilies =
		themeJson?.settings?.typography?.fontFamilies?.custom;

	let fontFamilies: any[] = [];
	if ( themeFontFamilies && customFontFamilies ) {
		fontFamilies = [ ...themeFontFamilies, ...customFontFamilies ];
	} else if ( themeFontFamilies ) {
		fontFamilies = themeFontFamilies;
	} else if ( customFontFamilies ) {
		fontFamilies = customFontFamilies;
	}
	const bodyFontFamilySetting = themeJson?.styles?.typography?.fontFamily;
	const bodyFontFamily = getFontFamilyFromSetting(
		fontFamilies,
		bodyFontFamilySetting
	);

	const headingFontFamilySetting =
		themeJson?.styles?.elements?.heading?.typography?.fontFamily;

	let headingFontFamily;
	if ( ! headingFontFamilySetting ) {
		headingFontFamily = bodyFontFamily;
	} else {
		headingFontFamily = getFontFamilyFromSetting(
			fontFamilies,
			themeJson?.styles?.elements?.heading?.typography?.fontFamily
		);
	}

	return [ bodyFontFamily, headingFontFamily ];
}

/**
 * Iterates through the presets array and searches for slugs that start with the specified
 * slugPrefix followed by a numerical suffix. It identifies the highest numerical suffix found
 * and returns one greater than the highest found suffix, ensuring that the new index is unique.
 *
 * @param presets    The array of preset objects, each potentially containing a slug property.
 * @param slugPrefix The prefix to look for in the preset slugs.
 *
 * @return The next available index for a preset with the specified slug prefix, or 1 if no matching slugs are found.
 */
/**
 * Gets the variation class name for a block style variation.
 *
 * @param variation The variation name.
 * @return The variation class name.
 */
export function getVariationClassName( variation: string ): string {
	if ( ! variation ) {
		return '';
	}
	return `is-style-${ variation }`;
}

export function getNewIndexFromPresets(
	presets: any[],
	slugPrefix: string
): number {
	const nameRegex = new RegExp( `^${ slugPrefix }([\\d]+)$` );
	const highestPresetValue = presets.reduce( ( currentHighest, preset ) => {
		if ( typeof preset?.slug === 'string' ) {
			const matches = preset?.slug.match( nameRegex );
			if ( matches ) {
				const id = parseInt( matches[ 1 ], 10 );
				if ( id > currentHighest ) {
					return id;
				}
			}
		}
		return currentHighest;
	}, 0 );
	return highestPresetValue + 1;
}

// Heading blocks render at any level, so every heading screen shares one list.
const HEADING_ELEMENTS = [ 'heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ];

// Has no block styles, so resolving with it leaves only the root and element
// layers, while `{ ref }` values can still point into `styles.blocks`.
const ELEMENT_ONLY_BLOCK_NAME = 'core/__element-only';

function getElementKeys( element: string ): string[] {
	return HEADING_ELEMENTS.includes( element )
		? HEADING_ELEMENTS
		: [ element ];
}

// State and viewport styles go to other selectors, so they cannot override
// the element itself.
function isOwnStylePath( path: string ) {
	return ! path.startsWith( ':' ) && ! path.startsWith( '@' );
}

const BOX_SIDES = [ 'top', 'right', 'bottom', 'left' ];

// Shorthands whose longhands live elsewhere in the tree. Nested longhands,
// such as `border.radius.topLeft` or `spacing.padding.top`, sit under their
// shorthand and match by path.
const SHORTHAND_LONGHANDS: Record< string, string[] > = {
	'border.color': BOX_SIDES.map( ( side ) => `border.${ side }.color` ),
	'border.style': BOX_SIDES.map( ( side ) => `border.${ side }.style` ),
	'border.width': BOX_SIDES.map( ( side ) => `border.${ side }.width` ),
	// Output as the `background` shorthand.
	'color.gradient': [
		'color.background',
		'background.backgroundImage',
		'background.backgroundPosition',
		'background.backgroundRepeat',
		'background.backgroundSize',
		'background.backgroundAttachment',
		'background.backgroundClip',
	],
};

function isSameOrNestedPath( path: string, otherPath: string ) {
	return (
		path === otherPath ||
		path.startsWith( `${ otherPath }.` ) ||
		otherPath.startsWith( `${ path }.` )
	);
}

// Whether two paths set any of the same CSS, whichever sets the shorthand.
function setsSameProperty( path: string, otherPath: string ) {
	const longhands = SHORTHAND_LONGHANDS[ path ] ?? [ path ];
	const otherLonghands = SHORTHAND_LONGHANDS[ otherPath ] ?? [ otherPath ];
	return longhands.some( ( longhand ) =>
		otherLonghands.some( ( otherLonghand ) =>
			isSameOrNestedPath( longhand, otherLonghand )
		)
	);
}

/**
 * The block types an element paints whose own Global Styles beat the element
 * for at least one property it sets. Block style variations are not counted.
 *
 * @param config     Merged Global Styles config.
 * @param element    Element key, e.g. `heading`, `h2` or `button`.
 * @param blockNames Block types that paint the element.
 * @return Block names, in the given order.
 */
export function getOverridingBlockTypes(
	config: GlobalStylesConfig | undefined,
	element: string,
	blockNames: string[]
): string[] {
	if ( ! blockNames.length || ! config?.styles ) {
		return [];
	}
	const elements = getElementKeys( element );
	const { sources: elementSources } = resolveStyle( config, {
		blockName: ELEMENT_ONLY_BLOCK_NAME,
		elements,
	} );
	const elementPaths = Object.keys( elementSources ).filter(
		( path ) => elementSources[ path ].layer === 'element'
	);
	return blockNames.filter( ( blockName ) => {
		const { sources } = resolveStyle( config, { blockName, elements } );
		return Object.keys( sources ).some(
			( path ) =>
				isOwnStylePath( path ) &&
				sources[ path ].layer === 'block' &&
				elementPaths.some( ( elementPath ) =>
					setsSameProperty( path, elementPath )
				)
		);
	} );
}
