/**
 * External Dependencies
 */
import { addFilter } from '@wordpress/hooks';

/**
 * Internal dependencies
 */
import './block';
import { ModalContentSettings, TriggerSettings, SpectraProCloseButtonChildOptions } from './controls';
import { withInspectorControls } from './edit';
const BLOCK_NAME_POPUP = 'spectra/modal';

/**
 * Add custom attributes to the modal block.
 *
 * @param {Object} settings The settings object.
 * @param {string} name     The name of the block.
 * @return {Object}         The modified settings object.
 */
addFilter( 
    'blocks.registerBlockType',
    'spectra-pro/modal/popup-attributes',
    ( settings, name ) => {
        if ( name !== BLOCK_NAME_POPUP ) {
            return settings;
        }

        return {
            ...settings,
            attributes: {
                ...settings.attributes,
                openModalAs: {
                    type: 'string',
                    default: 'popup'
                },
                appearEffect: {
                    type: 'string',
                    default: 'spectra-effect-default'
                },
                modalPosition: {
                    type: 'string',
                    default: 'centered'
                },
                hPos: {
                    type: 'number',
                    default: '',
                },
                vPos: {
                    type: 'number',
                    default: '',
                },
                cssClass: {
                    type: 'string',
                    default: '',
                },
                cssId: {
                    type: 'string',
                    default: '',
                },
                exitIntent: {
                    type: 'boolean',
                    default: false,
                },
                showAfterSeconds: {
                    type: 'boolean',
                    default: false,
                },
                noOfSecondsToShow: {
                    type: 'number',
                    default: 5,
                },
                enableCookies: {
                    type: 'boolean',
                    default: false,
                },
                setCookiesOn: {
                    type: 'string',
                    default: 'close-action',
                },
                hideForDays: {
                    type: 'number',
                    default: 2,
                },
            }
        };
    }
);

/**
 * Enhances the BlockEdit component for the "spectra/modal-child-trigger" block.
 *
 * Injects a placeholder message in the editor when the modal trigger type is 
 * "custom-class", "custom-id", or "automatic", allowing users to identify and 
 * edit modal-related settings more easily. This UI is editor-only and does not 
 * appear on the frontend.
 *
 * @since x.x.x
 *
 * @param {Function} BlockEdit - Original BlockEdit component.
 * @return {Function} Enhanced BlockEdit component with modal placeholder UI.
 */
addFilter( 'editor.BlockEdit', 'spectra/modal-trigger', withInspectorControls );

/**
 * Filters the icon position options for modal close buttons (Pro).
 *
 * Adds or disables specific close button icon positions based on modal attributes
 * such as `openModalAs` and `modalPosition`.
 *
 * @since x.x.x
 *
 * @param {Array} options - Original array of icon position options.
 * @return {Array} Modified array with updated disabled states.
 */
addFilter(
	'spectra-v3.modal-popup.styles.icon-position.children',
	'spectra-pro/close-button-icon-options',
	SpectraProCloseButtonChildOptions
);

/**
 * Enhances BlockEdit component with modal controls in the editor.
 *
 * @since x.x.x
 *
 * @param {Function} BlockEdit - The original BlockEdit component.
 * @return {Function} Enhanced BlockEdit component with modal controls.
 */
addFilter( 'spectra.modal-popup.options', 'spectra-pro/modal-popup-options', ModalContentSettings );

/**
 * Enhances BlockEdit component with modal trigger controls in the editor.
 *
 * @since x.x.x
 *
 * @param {Function} BlockEdit - The original BlockEdit component.
 * @return {Function} Enhanced BlockEdit component with modal trigger controls.
 */
addFilter( 'spectra.modal-child-trigger.options', 'spectra-pro/modal-child-trigger-options', TriggerSettings );
