/**
 * Simplyin Payment - Custom Email Availability Check
 * Checks both Magento and Simply.IN
 */
define([
    'mage/storage',
    'Magento_Checkout/js/model/url-builder'
], function (storage, urlBuilder) {
    'use strict';

    return function (deferred, email) {
        return storage.post(
            urlBuilder.createUrl('/customers/isEmailAvailable', {}),
            JSON.stringify({
                customerEmail: email
            }),
            false
        ).done(function (isEmailAvailable) {
            if (isEmailAvailable) {
                // Email available in Magento - resolve with response
                deferred.resolve({
                    isEmailAvailable: true,
                    email: email
                });
            } else {
                // Email exists in Magento - reject
                deferred.reject({
                    isEmailAvailable: false,
                    email: email
                });
            }
        }).fail(function () {
            deferred.reject({
                isEmailAvailable: false,
                email: email
            });
        });
    };
});
