import { forceUpdate, h } from "@stencil/core";
import { LedgerConnectEventsEnum } from "../ledger.types";
import { LedgerConnectBase } from "../LedgerConnectBase";
export class LedgerFlow {
    constructor() {
        this.selectedIndex = 0;
        this.data = {
            accountScreenData: null,
            confirmScreenData: null,
            connectScreenData: {},
        };
    }
    async getEventBus() {
        return this.ledgerConnectBase.getEventBus();
    }
    selectAccount(index) {
        this.ledgerConnectBase.selectAccount(index);
        this.selectedIndex = this.ledgerConnectBase.selectedIndex;
    }
    removeComponent() {
        var _a;
        if ((_a = this.hostElement) === null || _a === void 0 ? void 0 : _a.parentNode) {
            this.hostElement.parentNode.removeChild(this.hostElement);
        }
    }
    getEventSubscription() {
        return {
            closeFn: () => this.removeComponent(),
            forceUpdateFn: () => {
                this.data = this.ledgerConnectBase.data;
                forceUpdate(this);
            },
        };
    }
    componentWillLoad() {
        this.ledgerConnectBase = new LedgerConnectBase(this.data);
    }
    componentDidLoad() {
        this.ledgerConnectBase.subscribeEventBus(this.getEventSubscription());
    }
    disconnectedCallback() {
        this.ledgerConnectBase.eventBus.publish(LedgerConnectEventsEnum.UI_DISCONNECTED);
        this.ledgerConnectBase.unsubscribeEventBus(this.getEventSubscription());
    }
    handleIntroConnect(event) {
        event.preventDefault();
        this.ledgerConnectBase.eventBus.publish(LedgerConnectEventsEnum.CONNECT_DEVICE);
    }
    render() {
        if (this.data.accountScreenData) {
            return (h("mvx-ledger-addresses", { selectedIndex: this.selectedIndex, accountScreenData: this.data.accountScreenData, onAccessWallet: () => this.ledgerConnectBase.accessWallet(), onSelectAccount: (event) => this.selectAccount(event.detail), onPageChange: (event) => this.ledgerConnectBase.goToPage(event.detail) }));
        }
        if (this.data.confirmScreenData) {
            return h("mvx-ledger-confirm", { confirmScreenData: this.data.confirmScreenData });
        }
        return h("mvx-ledger-intro", { connectScreenData: this.data.connectScreenData, onConnect: this.handleIntroConnect.bind(this) });
    }
    static get is() { return "mvx-ledger-flow"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["ledger-flow.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["ledger-flow.css"]
        };
    }
    static get properties() {
        return {
            "data": {
                "type": "unknown",
                "attribute": "data",
                "mutable": false,
                "complexType": {
                    "original": "ILedgerConnectPanelData",
                    "resolved": "ILedgerConnectPanelData",
                    "references": {
                        "ILedgerConnectPanelData": {
                            "location": "import",
                            "path": "../ledger.types",
                            "id": "src/components/functional/ledger/ledger.types.ts::ILedgerConnectPanelData"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "defaultValue": "{\n    accountScreenData: null,\n    confirmScreenData: null,\n    connectScreenData: {},\n  }"
            }
        };
    }
    static get states() {
        return {
            "selectedIndex": {}
        };
    }
    static get methods() {
        return {
            "getEventBus": {
                "complexType": {
                    "signature": "() => Promise<IEventBus>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "IEventBus": {
                            "location": "import",
                            "path": "utils/EventBus",
                            "id": "src/utils/EventBus.ts::IEventBus"
                        }
                    },
                    "return": "Promise<IEventBus>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "hostElement"; }
}
//# sourceMappingURL=ledger-flow.js.map
