(() => {
    'use strict';

    /*----------------------------------------
    送信完了後にURLのクエリパラメータを削除
    ----------------------------------------*/
    const completedUrl = new URL(window.location.href);

    if (completedUrl.searchParams.has('im_mail_form_completed')) {

        completedUrl.searchParams.delete(
            'im_mail_form_completed'
        );

        window.history.replaceState(
            {},
            '',
            completedUrl.toString()
        );
    }

    document.addEventListener(
        'click',
        (event) => {

            const removeButton = event.target.closest(
                '[data-confirmed-file-remove]'
            );

            if (!removeButton) {
                return;
            }

            const selectedFile = removeButton.closest(
                '[data-confirmed-file]'
            );

            const control = removeButton.closest(
                '.im-mail-form__control'
            );

            if (
                !selectedFile ||
                !control
            ) {
                return;
            }

            const removeValue = control.querySelector(
                '[data-confirmed-file-remove-value]'
            );

            const fileInput = control.querySelector(
                '[data-contact-form-file-input]'
            );

            if (
                !removeValue ||
                !fileInput
            ) {
                return;
            }

            removeValue.value = '1';
            selectedFile.hidden = true;
            fileInput.hidden = false;

        }
    );

})();