// External dependencies.
import React, { ReactElement } from 'react';

// Divi dependencies.
import {
	StyleContainer,
	StylesProps,
} from '@divi/module';

// Local dependencies.
import { DynamicLinkAttrs } from './types';
import {
	createBorderStyles,
	createSpacingStyles,
	createTypographyStyles,
	extractBorderAttrs,
	extractSpasingAttrs,
	extractTypographyAttrs,
} from '../../utils';

const ModuleStyles = ({
	settings,
	mode,
	state,
	noStyleTag,
	elements,
	attrs,
	orderClass,
}: StylesProps<DynamicLinkAttrs>): ReactElement => {
	const selectors = {
		link: `${orderClass} .jet-listing-dynamic-link__link`,
		linkHover: `${orderClass} .jet-listing-dynamic-link__link:hover`,
		icon: `${orderClass} .jet-listing-dynamic-link__icon`,
		iconPath: `${orderClass} .jet-listing-dynamic-link__icon svg path`,
		iconHover: `${orderClass} .jet-listing-dynamic-link__link:hover .jet-listing-dynamic-link__icon`,
		iconPathHover: `${orderClass} .jet-listing-dynamic-link__link:hover .jet-listing-dynamic-link__icon svg path`,
		iconSvg: `${orderClass} .jet-listing-dynamic-link__icon svg`,
	};

	const linkTypography = extractTypographyAttrs(attrs?.module?.decoration?.linkTypography);
	const linkTypographyStyles = createTypographyStyles(selectors.link, linkTypography);

	const linkBorder = extractBorderAttrs(attrs?.module?.decoration?.linkBorder);
	const linkBorderStyles = createBorderStyles(selectors.link, linkBorder);

	const linkSpacing = extractSpasingAttrs(attrs?.module?.decoration?.linkSpacing);
	const linkSpacingStyles = createSpacingStyles(selectors.link, linkSpacing);

	return (
		<StyleContainer mode={mode} state={state} noStyleTag={noStyleTag}>
			{elements.style({
				attrName: 'module',
				styleProps: {
					disabledOn: {
						disabledModuleVisibility: settings?.disabledModuleVisibility,
					},
					advancedStyles: [
						...linkTypographyStyles,
						...linkBorderStyles,
						...linkSpacingStyles,
						{
							componentName: 'divi/common',
							props: {
								property: 'background-color',
								selector: selectors.link,
								attr: attrs?.module?.decoration?.linkBg,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'color',
								selector: selectors.link,
								attr: attrs?.module?.decoration?.linkColor,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'color',
								selector: selectors.icon,
								attr: attrs?.module?.decoration?.linkIconColor,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'fill',
								selector: selectors.iconPath,
								attr: attrs?.module?.decoration?.linkIconColor,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'background-color',
								selector: selectors.linkHover,
								attr: attrs?.module?.decoration?.linkBgHover,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'color',
								selector: selectors.linkHover,
								attr: attrs?.module?.decoration?.linkColorHover,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'color',
								selector: selectors.iconHover,
								attr: attrs?.module?.decoration?.linkIconColorHover,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'fill',
								selector: selectors.iconPathHover,
								attr: attrs?.module?.decoration?.linkIconColorHover,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'align-self',
								selector: selectors.link,
								attr: attrs?.module?.decoration?.linkAlignment,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'border-color',
								selector: selectors.link,
								attr: attrs?.module?.decoration?.linkBorderColor,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'border-color',
								selector: selectors.linkHover,
								attr: attrs?.module?.decoration?.linkHoverBorderColor,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'order',
								selector: selectors.icon,
								attr: attrs?.module?.decoration?.linkIconPosition,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'flex-direction',
								selector: selectors.link,
								attr: attrs?.module?.decoration?.linkIconOrientation,
							},
						},
						{
							componentName: 'divi/common',
							props: {
								property: 'width',
								selector: selectors.iconSvg,
								attr: attrs?.module?.decoration?.linkIconSize,
							},
						},
                        {
                            componentName: 'divi/common',
                            props: {
                                property: 'height',
                                selector: selectors.iconSvg,
                                attr: 'auto',
                            },
                        },
						{
							componentName: 'divi/common',
							props: {
								property: 'gap',
								selector: selectors.link,
								attr: attrs?.module?.decoration?.linkIconGap,
							},
						},
					],
				},
			})}
		</StyleContainer>
	);
};

export {
	ModuleStyles,
};
