// External Dependencies.
import React, { ReactElement, useEffect, useRef } from 'react';

// Divi Dependencies.
import {
	ModuleContainer,
	ElementComponents,
} from '@divi/module';

// Local Dependencies.
import { DatePeriodFilterEditProps } from './types';
import { ModuleStyles } from './styles';
import ServerSideRender from '../../server-side-render';
import { convertAttributes } from '../../utils';
import { __ } from '@wordpress/i18n';
import { ModuleScriptData } from './module-script-data';
import { moduleClassnames } from './module-classnames';

/**
 * Static Module edit component of visual builder.
 *
 * @since ??
 *
 * @param {DatePeriodFilterEditProps} props React component props.
 *
 * @returns {ReactElement}
 */
const DatePeriodFilterEdit = (props: DatePeriodFilterEditProps): ReactElement => {
	const {
		attrs,
		id,
		name,
		elements,
	} = props;

	const ensureAirDatepickerLanguage = () => {
		const jq = ( window as any ).jQuery || ( window as any ).$;
		const airDatepicker = jq?.fn?.airDatepicker;

		if ( airDatepicker && ! airDatepicker.language ) {
			airDatepicker.language = {};
		}
	};

	ensureAirDatepickerLanguage();

	return (
		<ModuleContainer
			attrs={attrs}
			elements={elements}
			id={id}
			name={name}
			stylesComponent={ModuleStyles}
			classnamesFunction={moduleClassnames}
			scriptDataComponent={ModuleScriptData}
		>
			{elements.styleComponents({
				attrName: 'module',
			})}
			{
				<>
					<ElementComponents
						attrs={attrs?.module?.decoration ?? {}}
						id={id}
					/>
					<ServerSideRender
						block="jet-smart-filters-divi/date-period-filter"
						attributes={ attrs }
					/>
				</>
			}
		</ModuleContainer>
	);
}

export {
	DatePeriodFilterEdit,
};
