const formType = 'aa-ft-simple-file';

function initPlugin(el, defaultOptions, libs) {
    var wrapClass = 'custom-file';
    var formWrap = el.classList.contains(wrapClass) ? el : el.closest('.' + wrapClass);

    if (formWrap === null) {
        console.error('could not create file plugin');
        return;
    }
    var fileInput = formWrap.querySelector(':scope .' + formType);

    var createPlugin = function () {
        return {
            wrap: formWrap,
            fileInput: fileInput,
            init() {
                this.fileInput.addEventListener('change', plugin.fileInputChangeListener.bind(this));
            },
            fileInputChangeListener(e) {
                var input = e.target;
                input.nextElementSibling.innerText = [...input.files].map((f) => f.name).join(', ');
            },
        }
    };


    var plugin = createPlugin();
    plugin.init();

    return plugin;
}

AAForm.addTypeHandler(formType, initPlugin);
