'use strict';

var swapModule_entry = require('./swap-module-BZ0C_ywD.js');
var index$1 = require('./index-CAgSYWnN.js');
var index = require('./index-DAb3o2f5.js');
require('./index-Mw_M0XDo.js');
require('./index-Bq66f3r7.js');
var ref = require('./ref-DoziWPoT.js');
require('./index-D67PsI9D.js');
require('./index-DpWtRdOC.js');
require('./index-kE_lp0w-.js');
require('buffer');
require('events');
require('ox');
require('viem');
require('viem/chains');
require('./index-U1WZx3oG.js');
require('./if-defined-C8-lLqpT.js');
require('./index-l_GugSCx.js');

var __decorate$5 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
exports.W3mEmailVerifyOtpView = class W3mEmailVerifyOtpView extends index.W3mEmailOtpWidget {
    constructor() {
        super(...arguments);
        this.onOtpSubmit = async (otp) => {
            try {
                if (this.authConnector) {
                    const namespace = swapModule_entry.ChainController.state.activeChain;
                    const connectionsByNamespace = swapModule_entry.ConnectionController.getConnections(namespace);
                    const isMultiWalletEnabled = swapModule_entry.OptionsController.state.remoteFeatures?.multiWallet;
                    const hasConnections = connectionsByNamespace.length > 0;
                    await this.authConnector.provider.connectOtp({ otp });
                    swapModule_entry.EventsController.sendEvent({ type: 'track', event: 'EMAIL_VERIFICATION_CODE_PASS' });
                    if (namespace) {
                        await swapModule_entry.ConnectionController.connectExternal(this.authConnector, namespace);
                    }
                    else {
                        throw new Error('Active chain is not set on ChainController');
                    }
                    if (swapModule_entry.OptionsController.state.remoteFeatures?.emailCapture) {
                        return;
                    }
                    if (swapModule_entry.OptionsController.state.siwx) {
                        swapModule_entry.ModalController.close();
                        return;
                    }
                    if (hasConnections && isMultiWalletEnabled) {
                        swapModule_entry.RouterController.replace('ProfileWallets');
                        swapModule_entry.SnackController.showSuccess('New Wallet Added');
                        return;
                    }
                    swapModule_entry.ModalController.close();
                }
            }
            catch (error) {
                swapModule_entry.EventsController.sendEvent({
                    type: 'track',
                    event: 'EMAIL_VERIFICATION_CODE_FAIL',
                    properties: { message: swapModule_entry.CoreHelperUtil.parseError(error) }
                });
                throw error;
            }
        };
        this.onOtpResend = async (email) => {
            if (this.authConnector) {
                await this.authConnector.provider.connectEmail({ email });
                swapModule_entry.EventsController.sendEvent({ type: 'track', event: 'EMAIL_VERIFICATION_CODE_SENT' });
            }
        };
    }
};
exports.W3mEmailVerifyOtpView = __decorate$5([
    index$1.customElement('w3m-email-verify-otp-view')
], exports.W3mEmailVerifyOtpView);

var styles$1 = swapModule_entry.css `
  wui-icon-box {
    height: ${({ spacing }) => spacing['16']};
    width: ${({ spacing }) => spacing['16']};
  }
`;

var __decorate$4 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
exports.W3mEmailVerifyDeviceView = class W3mEmailVerifyDeviceView extends swapModule_entry.i {
    constructor() {
        super();
        this.email = swapModule_entry.RouterController.state.data?.email;
        this.authConnector = swapModule_entry.ConnectorController.getAuthConnector();
        this.loading = false;
        this.listenForDeviceApproval();
    }
    render() {
        if (!this.email) {
            throw new Error('w3m-email-verify-device-view: No email provided');
        }
        if (!this.authConnector) {
            throw new Error('w3m-email-verify-device-view: No auth connector provided');
        }
        return swapModule_entry.b `
      <wui-flex
        flexDirection="column"
        alignItems="center"
        .padding=${['6', '3', '6', '3']}
        gap="4"
      >
        <wui-icon-box size="xl" color="accent-primary" icon="sealCheck"></wui-icon-box>

        <wui-flex flexDirection="column" alignItems="center" gap="3">
          <wui-flex flexDirection="column" alignItems="center">
            <wui-text variant="md-regular" color="primary">
              Approve the login link we sent to
            </wui-text>
            <wui-text variant="md-regular" color="primary"><b>${this.email}</b></wui-text>
          </wui-flex>

          <wui-text variant="sm-regular" color="secondary" align="center">
            The code expires in 20 minutes
          </wui-text>

          <wui-flex alignItems="center" id="w3m-resend-section" gap="2">
            <wui-text variant="sm-regular" color="primary" align="center">
              Didn't receive it?
            </wui-text>
            <wui-link @click=${this.onResendCode.bind(this)} .disabled=${this.loading}>
              Resend email
            </wui-link>
          </wui-flex>
        </wui-flex>
      </wui-flex>
    `;
    }
    async listenForDeviceApproval() {
        if (this.authConnector) {
            try {
                await this.authConnector.provider.connectDevice();
                swapModule_entry.EventsController.sendEvent({ type: 'track', event: 'DEVICE_REGISTERED_FOR_EMAIL' });
                swapModule_entry.EventsController.sendEvent({ type: 'track', event: 'EMAIL_VERIFICATION_CODE_SENT' });
                swapModule_entry.RouterController.replace('EmailVerifyOtp', { email: this.email });
            }
            catch (error) {
                swapModule_entry.RouterController.goBack();
            }
        }
    }
    async onResendCode() {
        try {
            if (!this.loading) {
                if (!this.authConnector || !this.email) {
                    throw new Error('w3m-email-login-widget: Unable to resend email');
                }
                this.loading = true;
                await this.authConnector.provider.connectEmail({ email: this.email });
                this.listenForDeviceApproval();
                swapModule_entry.SnackController.showSuccess('Code email resent');
            }
        }
        catch (error) {
            swapModule_entry.SnackController.showError(error);
        }
        finally {
            this.loading = false;
        }
    }
};
exports.W3mEmailVerifyDeviceView.styles = styles$1;
__decorate$4([
    index$1.r()
], exports.W3mEmailVerifyDeviceView.prototype, "loading", void 0);
exports.W3mEmailVerifyDeviceView = __decorate$4([
    index$1.customElement('w3m-email-verify-device-view')
], exports.W3mEmailVerifyDeviceView);

var styles = swapModule_entry.i$1 `
  wui-email-input {
    width: 100%;
  }

  form {
    width: 100%;
    display: block;
    position: relative;
  }
`;

var __decorate$3 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
exports.W3mUpdateEmailWalletView = class W3mUpdateEmailWalletView extends swapModule_entry.i {
    constructor() {
        super(...arguments);
        this.formRef = ref.e();
        this.initialEmail = swapModule_entry.RouterController.state.data?.email ?? '';
        this.redirectView = swapModule_entry.RouterController.state.data?.redirectView;
        this.email = '';
        this.loading = false;
    }
    firstUpdated() {
        this.formRef.value?.addEventListener('keydown', event => {
            if (event.key === 'Enter') {
                this.onSubmitEmail(event);
            }
        });
    }
    render() {
        return swapModule_entry.b `
      <wui-flex flexDirection="column" padding="4" gap="4">
        <form ${ref.n(this.formRef)} @submit=${this.onSubmitEmail.bind(this)}>
          <wui-email-input
            value=${this.initialEmail}
            .disabled=${this.loading}
            @inputChange=${this.onEmailInputChange.bind(this)}
          >
          </wui-email-input>
          <input type="submit" hidden />
        </form>
        ${this.buttonsTemplate()}
      </wui-flex>
    `;
    }
    onEmailInputChange(event) {
        this.email = event.detail;
    }
    async onSubmitEmail(event) {
        try {
            if (this.loading) {
                return;
            }
            this.loading = true;
            event.preventDefault();
            const authConnector = swapModule_entry.ConnectorController.getAuthConnector();
            if (!authConnector) {
                throw new Error('w3m-update-email-wallet: Auth connector not found');
            }
            const response = await authConnector.provider.updateEmail({ email: this.email });
            swapModule_entry.EventsController.sendEvent({ type: 'track', event: 'EMAIL_EDIT' });
            if (response.action === 'VERIFY_SECONDARY_OTP') {
                swapModule_entry.RouterController.push('UpdateEmailSecondaryOtp', {
                    email: this.initialEmail,
                    newEmail: this.email,
                    redirectView: this.redirectView
                });
            }
            else {
                swapModule_entry.RouterController.push('UpdateEmailPrimaryOtp', {
                    email: this.initialEmail,
                    newEmail: this.email,
                    redirectView: this.redirectView
                });
            }
        }
        catch (error) {
            swapModule_entry.SnackController.showError(error);
            this.loading = false;
        }
    }
    buttonsTemplate() {
        const showSubmit = !this.loading && this.email.length > 3 && this.email !== this.initialEmail;
        if (!this.redirectView) {
            return swapModule_entry.b `
        <wui-button
          size="md"
          variant="accent-primary"
          fullWidth
          @click=${this.onSubmitEmail.bind(this)}
          .disabled=${!showSubmit}
          .loading=${this.loading}
        >
          Save
        </wui-button>
      `;
        }
        return swapModule_entry.b `
      <wui-flex gap="3">
        <wui-button size="md" variant="neutral" fullWidth @click=${swapModule_entry.RouterController.goBack}>
          Cancel
        </wui-button>

        <wui-button
          size="md"
          variant="accent-primary"
          fullWidth
          @click=${this.onSubmitEmail.bind(this)}
          .disabled=${!showSubmit}
          .loading=${this.loading}
        >
          Save
        </wui-button>
      </wui-flex>
    `;
    }
};
exports.W3mUpdateEmailWalletView.styles = styles;
__decorate$3([
    index$1.r()
], exports.W3mUpdateEmailWalletView.prototype, "email", void 0);
__decorate$3([
    index$1.r()
], exports.W3mUpdateEmailWalletView.prototype, "loading", void 0);
exports.W3mUpdateEmailWalletView = __decorate$3([
    index$1.customElement('w3m-update-email-wallet-view')
], exports.W3mUpdateEmailWalletView);

var __decorate$2 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
exports.W3mUpdateEmailPrimaryOtpView = class W3mUpdateEmailPrimaryOtpView extends index.W3mEmailOtpWidget {
    constructor() {
        super();
        this.email = swapModule_entry.RouterController.state.data?.email;
        this.onOtpSubmit = async (otp) => {
            try {
                if (this.authConnector) {
                    await this.authConnector.provider.updateEmailPrimaryOtp({ otp });
                    swapModule_entry.EventsController.sendEvent({ type: 'track', event: 'EMAIL_VERIFICATION_CODE_PASS' });
                    swapModule_entry.RouterController.replace('UpdateEmailSecondaryOtp', swapModule_entry.RouterController.state.data);
                }
            }
            catch (error) {
                swapModule_entry.EventsController.sendEvent({
                    type: 'track',
                    event: 'EMAIL_VERIFICATION_CODE_FAIL',
                    properties: { message: swapModule_entry.CoreHelperUtil.parseError(error) }
                });
                throw error;
            }
        };
        this.onStartOver = () => {
            swapModule_entry.RouterController.replace('UpdateEmailWallet', swapModule_entry.RouterController.state.data);
        };
    }
};
exports.W3mUpdateEmailPrimaryOtpView = __decorate$2([
    index$1.customElement('w3m-update-email-primary-otp-view')
], exports.W3mUpdateEmailPrimaryOtpView);

var __decorate$1 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
exports.W3mUpdateEmailSecondaryOtpView = class W3mUpdateEmailSecondaryOtpView extends index.W3mEmailOtpWidget {
    constructor() {
        super();
        this.email = swapModule_entry.RouterController.state.data?.newEmail;
        this.redirectView = swapModule_entry.RouterController.state.data?.redirectView;
        this.onOtpSubmit = async (otp) => {
            try {
                if (this.authConnector) {
                    await this.authConnector.provider.updateEmailSecondaryOtp({ otp });
                    swapModule_entry.EventsController.sendEvent({ type: 'track', event: 'EMAIL_VERIFICATION_CODE_PASS' });
                    if (this.redirectView) {
                        swapModule_entry.RouterController.reset(this.redirectView);
                    }
                }
            }
            catch (error) {
                swapModule_entry.EventsController.sendEvent({
                    type: 'track',
                    event: 'EMAIL_VERIFICATION_CODE_FAIL',
                    properties: { message: swapModule_entry.CoreHelperUtil.parseError(error) }
                });
                throw error;
            }
        };
        this.onStartOver = () => {
            swapModule_entry.RouterController.replace('UpdateEmailWallet', swapModule_entry.RouterController.state.data);
        };
    }
};
exports.W3mUpdateEmailSecondaryOtpView = __decorate$1([
    index$1.customElement('w3m-update-email-secondary-otp-view')
], exports.W3mUpdateEmailSecondaryOtpView);

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
exports.W3mEmailLoginView = class W3mEmailLoginView extends swapModule_entry.i {
    constructor() {
        super();
        this.authConnector = swapModule_entry.ConnectorController.getAuthConnector();
        this.isEmailEnabled = swapModule_entry.OptionsController.state.remoteFeatures?.email;
        this.isAuthEnabled = this.checkIfAuthEnabled(swapModule_entry.ConnectorController.state.connectors);
        this.connectors = swapModule_entry.ConnectorController.state.connectors;
        swapModule_entry.ConnectorController.subscribeKey('connectors', val => {
            this.connectors = val;
            this.isAuthEnabled = this.checkIfAuthEnabled(this.connectors);
        });
    }
    render() {
        if (!this.isEmailEnabled) {
            throw new Error('w3m-email-login-view: Email is not enabled');
        }
        if (!this.isAuthEnabled) {
            throw new Error('w3m-email-login-view: No auth connector provided');
        }
        return swapModule_entry.b `<wui-flex flexDirection="column" .padding=${['1', '3', '3', '3']} gap="4">
      <w3m-email-login-widget></w3m-email-login-widget>
    </wui-flex> `;
    }
    checkIfAuthEnabled(connectors) {
        const namespacesWithAuthConnector = connectors
            .filter(c => c.type === swapModule_entry.ConstantsUtil$2.CONNECTOR_TYPE_AUTH)
            .map(i => i.chain);
        const authSupportedNamespaces = swapModule_entry.ConstantsUtil$1.AUTH_CONNECTOR_SUPPORTED_CHAINS;
        return authSupportedNamespaces.some(ns => namespacesWithAuthConnector.includes(ns));
    }
};
__decorate([
    index$1.r()
], exports.W3mEmailLoginView.prototype, "connectors", void 0);
exports.W3mEmailLoginView = __decorate([
    index$1.customElement('w3m-email-login-view')
], exports.W3mEmailLoginView);

Object.defineProperty(exports, "W3mEmailOtpWidget", {
    enumerable: true,
    get: function () { return index.W3mEmailOtpWidget; }
});
//# sourceMappingURL=email-Csb9vAPD.js.map

//# sourceMappingURL=email-Csb9vAPD.js.map