(function () {

    const DC = {

        emailAddress: null,
        successSpan: null,
        errorSpan: null,

        onReady: function DC_onReady() {

            document.addEventListener('DOMContentLoaded', () => {
                const scope = this,
                    button = document.getElementById("dc-submit-subscribe-btn");

                if (button) {
                    const inputId = button.dataset.input;
                    scope.emailAddress = document.getElementById(inputId);
                    scope.successSpan = document.getElementById("dc-subscribe-success-msg");
                    scope.errorSpan = document.getElementById("dc-subscribe-error-msg");

                    button.addEventListener('click', function (e) {
                        e.preventDefault();
                        scope.subscribe(scope, this);
                    });
                }
            });
        },

        subscribe: function DC_subscribe(scope, button) {
            if (scope && button) {
                const url = button.dataset.url;
                scope.sendRequest(scope, url, 'POST');
            }
        },

        sendRequest: function DC_sendRequest(scope, url, method) {
            if (scope && url && method) {

                DC.hideElement(scope.successSpan);
                DC.hideElement(scope.errorSpan);

                if (scope.emailAddress) {
                    if (!scope.emailAddress.value || scope.emailAddress.value === "") {
                        DC.renderMessage(scope.errorSpan, 'Email\x20shouldn\x27t\x20be\x20empty');
                    } else {

                        Liferay.Util.fetch(url, {
                            method: method,
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify({"emailAddress": scope.emailAddress.value})
                        }).then((response) => {
                            return response.json();
                        }).then(response => {
                            if (response && response.success) {
                                DC.renderMessage(scope.successSpan, response.success);
                                if (scope.emailAddress) {
                                    scope.emailAddress.value = "";
                                }
                            }
                            if (response && response.error) {
                                DC.renderMessage(scope.errorSpan, response.error);
                            }
                        })
                            .catch(error => {
                                DC.renderMessage(scope.errorSpan, 'Couldn\x27t\x20subscribe\x2e\x20There\x20is\x20some\x20internal\x20issue\x2e');
                                console.error('Error:', error);
                            });
                    }
                }
            }
        },

        hideElement: function DC_hideElement(htmlEl) {
            if (htmlEl) {
                htmlEl.classList.add('dc-hidden');
            }
        },

        renderMessage: function DC_renderMessage(htmlEl, message) {
            if (htmlEl) {
                htmlEl.textContent = message;
                htmlEl.classList.remove('dc-hidden');
            }
        }
    };

    DC.onReady();

})();

