import IGDATAPOOLFE from '@dp/ig_datapool_fe.js';
//import flatpickr from 'flatpickr/index.js';

const dateFieldPlugin = {
    initFields(form) {
        this.init(form);
    },

    init(form) {
        this.form = form;

        this.form.querySelectorAll('.dp-flatpickr').forEach((input) => {
            // Read options from data-dateoptions (JSON)
            const options = input.dataset.dateoptions ? JSON.parse(input.dataset.dateoptions) : {};

            // Initialize flatpickr
            flatpickr(input, options);

            // Auto-submit on change if required
            if (input.dataset.submit !== undefined) {
                input.addEventListener('change', () => {
                    const parentForm = input.closest('form');
                    if (parentForm) parentForm.submit();
                });
            }
        });
    }
};

IGDATAPOOLFE.addPlugin('dateField', dateFieldPlugin);
export default dateFieldPlugin;