/**
 * External Dependencies
 */
import {
    EffectCoverflow,
    EffectFade,
    EffectFlip,
    EffectCube
} from 'swiper/modules';
import { addFilter } from '@wordpress/hooks';

// Import required Swiper CSS.
import 'swiper/css/effect-cube';
import 'swiper/css/effect-coverflow';
import 'swiper/css/effect-fade';
import 'swiper/css/effect-flip';

// Module mapping.
const EFFECT_MODULES = {
    cube: EffectCube,
    coverflow: EffectCoverflow,
    fade: EffectFade,
    flip: EffectFlip
};

// Add modules to the modules array.
addFilter(
    'spectra.sliderModules',
    'spectra-pro/slider/modules',
    ( modules, params ) => {
        const effect = params?.effect;
        if ( effect && EFFECT_MODULES[effect] ) {
            modules.push( EFFECT_MODULES[effect] );
        }
        return modules;
    }
);

// Add effect parameters.
addFilter(
    'spectra.sliderParams',
    'spectra-pro/slider/params',
    ( params ) => {
        const effect = params?.effect;
        
        if ( !effect || !EFFECT_MODULES[effect] ) {
            return params;
        }

        // Configure effect-specific parameters
        switch ( effect ) {
            case 'cube':
                params.cubeEffect = {
                    slideShadows: true,
                };
                break;

            case 'coverflow':
                params.coverflowEffect = {
                    rotate: 50,
                    stretch: 0,
                    depth: 100,
                    modifier: 1,
                };
                params.centeredSlides = true;
                break;

            case 'flip':
                params.flipEffect = {
                    limitRotation: true,
                };
                break;

            case 'fade':
                params.fadeEffect = {
                    crossFade: true,
                };
                break;
        }

        // Handle slides per view for special effects.
        if ( ['cube', 'flip', 'fade'].includes( effect ) ) {
            params.slidesPerView = 1;
            if ( params.breakpoints ) {
                Object.keys( params.breakpoints ).forEach( key => {
                    params.breakpoints[key].slidesPerView = 1;
                } );
            }
        }

        return params;
    }
);
