import { r as registerInstance, h, f as forceUpdate, a as getElement } from './index-fb899b9a.js';
import { L as LedgerConnectBase, a as LedgerConnectEventsEnum } from './LedgerConnectBase-5b3f65a4.js';
import './EventBus-56ba7d6d.js';

const LedgerConnect = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.data = {
            accountScreenData: null,
            confirmScreenData: null,
            connectScreenData: {},
        };
        this.selectedIndex = 0;
    }
    componentWillLoad() {
        this.ledgerConnectBase = new LedgerConnectBase(this.data);
    }
    async getEventBus() {
        return this.ledgerConnectBase.getEventBus();
    }
    render() {
        const { accountScreenData, confirmScreenData, connectScreenData } = this.data;
        if (accountScreenData) {
            return (h("ledger-account-screen", { accountScreenData: accountScreenData, selectedIndex: this.selectedIndex, onSelectAccount: (event) => this.selectAccount(event.detail), onAccessWallet: () => this.accessWallet(), onPrevPage: () => this.prevPage(), onNextPage: () => this.nextPage() }));
        }
        if (confirmScreenData) {
            return h("ledger-confirm-screen", { confirmScreenData: confirmScreenData });
        }
        return h("ledger-connect-screen", { connectScreenData: connectScreenData, onConnect: () => this.ledgerConnectBase.eventBus.publish(LedgerConnectEventsEnum.CONNECT_DEVICE) });
    }
    accessWallet() {
        this.ledgerConnectBase.accessWallet();
    }
    selectAccount(index) {
        this.ledgerConnectBase.selectAccount(index);
        // this is needed for the UI to be reactive
        this.selectedIndex = this.ledgerConnectBase.selectedIndex;
    }
    async nextPage() {
        this.ledgerConnectBase.nextPage();
    }
    async prevPage() {
        this.ledgerConnectBase.prevPage();
    }
    removeComponent() {
        var _a;
        if ((_a = this.hostElement) === null || _a === void 0 ? void 0 : _a.parentNode) {
            this.hostElement.parentNode.removeChild(this.hostElement);
        }
    }
    componentDidLoad() {
        this.ledgerConnectBase.subscribeEventBus({
            closeFn: () => this.removeComponent(),
            forceUpdateFn: () => {
                // this is needed for the UI to be reactive
                this.data = this.ledgerConnectBase.data;
                forceUpdate(this);
            },
        });
    }
    disconnectedCallback() {
        this.ledgerConnectBase.unsubscribeEventBus({
            closeFn: () => this.removeComponent(),
            forceUpdateFn: () => {
                // this is needed for the UI to be reactive
                this.data = this.ledgerConnectBase.data;
                forceUpdate(this);
            },
        });
    }
    get hostElement() { return getElement(this); }
};

export { LedgerConnect as ledger_connect };

//# sourceMappingURL=ledger-connect.entry.js.map