'use strict';

var swapModule_entry = require('./swap-module-BZ0C_ywD.js');
var index = require('./index-CAgSYWnN.js');
var ifDefined = require('./if-defined-C8-lLqpT.js');

function getPopupWindow() {
    try {
        return swapModule_entry.CoreHelperUtil.returnOpenHref(`${swapModule_entry.ConstantsUtil$1.SECURE_SITE_SDK_ORIGIN}/loading`, 'popupWindow', 'width=600,height=800,scrollbars=yes');
    }
    catch (error) {
        throw new Error('Could not open social popup');
    }
}
async function connectFarcaster() {
    swapModule_entry.RouterController.push('ConnectingFarcaster');
    const authConnector = swapModule_entry.ConnectorController.getAuthConnector();
    if (authConnector) {
        const accountData = swapModule_entry.ChainController.getAccountData();
        if (!accountData?.farcasterUrl) {
            try {
                const { url } = await authConnector.provider.getFarcasterUri();
                swapModule_entry.ChainController.setAccountProp('farcasterUrl', url, swapModule_entry.ChainController.state.activeChain);
            }
            catch (error) {
                swapModule_entry.RouterController.goBack();
                swapModule_entry.SnackController.showError(error);
            }
        }
    }
}
async function connectSocial(socialProvider) {
    swapModule_entry.RouterController.push('ConnectingSocial');
    const authConnector = swapModule_entry.ConnectorController.getAuthConnector();
    let popupWindow = null;
    try {
        const timeout = setTimeout(() => {
            throw new Error('Social login timed out. Please try again.');
        }, 45_000);
        if (authConnector && socialProvider) {
            if (!swapModule_entry.CoreHelperUtil.isTelegram()) {
                popupWindow = getPopupWindow();
            }
            if (popupWindow) {
                swapModule_entry.ChainController.setAccountProp('socialWindow', swapModule_entry.ref(popupWindow), swapModule_entry.ChainController.state.activeChain);
            }
            else if (!swapModule_entry.CoreHelperUtil.isTelegram()) {
                throw new Error('Could not create social popup');
            }
            const { uri } = await authConnector.provider.getSocialRedirectUri({
                provider: socialProvider
            });
            if (!uri) {
                popupWindow?.close();
                throw new Error('Could not fetch the social redirect uri');
            }
            if (popupWindow) {
                popupWindow.location.href = uri;
            }
            if (swapModule_entry.CoreHelperUtil.isTelegram()) {
                swapModule_entry.StorageUtil.setTelegramSocialProvider(socialProvider);
                const parsedUri = swapModule_entry.CoreHelperUtil.formatTelegramSocialLoginUrl(uri);
                swapModule_entry.CoreHelperUtil.openHref(parsedUri, '_top');
            }
            clearTimeout(timeout);
        }
    }
    catch (error) {
        popupWindow?.close();
        const errorMessage = swapModule_entry.CoreHelperUtil.parseError(error);
        swapModule_entry.SnackController.showError(errorMessage);
        swapModule_entry.EventsController.sendEvent({
            type: 'track',
            event: 'SOCIAL_LOGIN_ERROR',
            properties: { provider: socialProvider, message: errorMessage }
        });
    }
}
async function executeSocialLogin(socialProvider) {
    swapModule_entry.ChainController.setAccountProp('socialProvider', socialProvider, swapModule_entry.ChainController.state.activeChain);
    swapModule_entry.EventsController.sendEvent({
        type: 'track',
        event: 'SOCIAL_LOGIN_STARTED',
        properties: { provider: socialProvider }
    });
    if (socialProvider === 'farcaster') {
        await connectFarcaster();
    }
    else {
        await connectSocial(socialProvider);
    }
}

var styles$1 = swapModule_entry.css `
  :host {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 40px;
    height: 40px;
    border-radius: ${({ borderRadius }) => borderRadius['20']};
    overflow: hidden;
  }

  wui-icon {
    width: 100%;
    height: 100%;
  }
`;

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;
};
let WuiLogo = class WuiLogo extends swapModule_entry.i {
    constructor() {
        super(...arguments);
        this.logo = 'google';
    }
    render() {
        return swapModule_entry.b `<wui-icon color="inherit" size="inherit" name=${this.logo}></wui-icon> `;
    }
};
WuiLogo.styles = [swapModule_entry.resetStyles, styles$1];
__decorate$1([
    index.n()
], WuiLogo.prototype, "logo", void 0);
WuiLogo = __decorate$1([
    index.customElement('wui-logo')
], WuiLogo);

var styles = swapModule_entry.css `
  :host {
    width: 100%;
  }

  button {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: ${({ spacing }) => spacing[3]};
    width: 100%;
    background-color: transparent;
    border-radius: ${({ borderRadius }) => borderRadius[4]};
  }

  wui-text {
    text-transform: capitalize;
  }

  @media (hover: hover) {
    button:hover:enabled {
      background-color: ${({ tokens }) => tokens.theme.foregroundPrimary};
    }
  }

  button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
`;

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;
};
let WuiListSocial = class WuiListSocial extends swapModule_entry.i {
    constructor() {
        super(...arguments);
        this.logo = 'google';
        this.name = 'Continue with google';
        this.disabled = false;
    }
    render() {
        return swapModule_entry.b `
      <button ?disabled=${this.disabled} tabindex=${ifDefined.o(this.tabIdx)}>
        <wui-flex gap="2" alignItems="center">
          <wui-image ?boxed=${true} logo=${this.logo}></wui-image>
          <wui-text variant="lg-regular" color="primary">${this.name}</wui-text>
        </wui-flex>
        <wui-icon name="chevronRight" size="lg" color="default"></wui-icon>
      </button>
    `;
    }
};
WuiListSocial.styles = [swapModule_entry.resetStyles, swapModule_entry.elementStyles, styles];
__decorate([
    index.n()
], WuiListSocial.prototype, "logo", void 0);
__decorate([
    index.n()
], WuiListSocial.prototype, "name", void 0);
__decorate([
    index.n()
], WuiListSocial.prototype, "tabIdx", void 0);
__decorate([
    index.n({ type: Boolean })
], WuiListSocial.prototype, "disabled", void 0);
WuiListSocial = __decorate([
    index.customElement('wui-list-social')
], WuiListSocial);

exports.executeSocialLogin = executeSocialLogin;
//# sourceMappingURL=index-DSRv56w1.js.map

//# sourceMappingURL=index-DSRv56w1.js.map