import { forceUpdate, h } from "@stencil/core";
import { DataTestIdsEnum } from "../../../constants/dataTestIds.enum";
import { LedgerConnectEventsEnum } from "./ledger-connect.types";
import { LedgerConnectBase } from "./LedgerConnectBase";
export class LedgerConnectModal {
    constructor() {
        this.data = {
            accountScreenData: null,
            confirmScreenData: null,
            connectScreenData: {},
        };
        this.selectedIndex = 0;
        this.getModalProps = () => {
            const { accountScreenData, confirmScreenData, connectScreenData } = this.data;
            if (accountScreenData) {
                return {
                    modalTitle: h("div", { "data-testid": `${DataTestIdsEnum.addressTableContainer}Title` }, "Access your wallet"),
                    modalSubtitle: h("div", { "data-testid": `${DataTestIdsEnum.addressTableContainer}SubTitle` }, "Choose the wallet you want to access"),
                    body: (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 {
                    modalTitle: 'Confirm',
                    modalSubtitle: 'Confirm Ledger Address',
                    body: h("ledger-confirm-screen", { confirmScreenData: confirmScreenData }),
                };
            }
            return {
                modalTitle: 'Connect Ledger',
                modalSubtitle: 'Unlock your device & open the MultiversX App',
                body: h("ledger-connect-screen", { connectScreenData: connectScreenData, onConnect: () => this.ledgerConnectBase.eventBus.publish(LedgerConnectEventsEnum.CONNECT_DEVICE) }),
            };
        };
    }
    componentWillLoad() {
        this.ledgerConnectBase = new LedgerConnectBase(this.data);
    }
    async getEventBus() {
        return this.ledgerConnectBase.getEventBus();
    }
    render() {
        const modalProps = this.getModalProps();
        return h("generic-modal", Object.assign({ key: 'd01d92ca0fcc5501f254543498ab565ce2007361' }, modalProps, { onClose: () => this.close() }));
    }
    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();
    }
    close(props = { isUserClick: true }) {
        var _a;
        if (props.isUserClick) {
            this.ledgerConnectBase.eventBus.publish(LedgerConnectEventsEnum.CLOSE);
        }
        if ((_a = this.hostElement) === null || _a === void 0 ? void 0 : _a.parentNode) {
            this.hostElement.parentNode.removeChild(this.hostElement);
        }
    }
    componentDidLoad() {
        this.ledgerConnectBase.subscribeEventBus({
            closeFn: () => this.close({ isUserClick: false }),
            forceUpdateFn: () => {
                // this is needed for the UI to be reactive
                this.data = this.ledgerConnectBase.data;
                forceUpdate(this);
            },
        });
    }
    disconnectedCallback() {
        this.ledgerConnectBase.unsubscribeEventBus({
            closeFn: () => this.close({ isUserClick: false }),
            forceUpdateFn: () => {
                // this is needed for the UI to be reactive
                this.data = this.ledgerConnectBase.data;
                forceUpdate(this);
            },
        });
    }
    static get is() { return "ledger-connect-modal"; }
    static get encapsulation() { return "shadow"; }
    static get properties() {
        return {
            "data": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "ILedgerConnectModalData",
                    "resolved": "ILedgerConnectModalData",
                    "references": {
                        "ILedgerConnectModalData": {
                            "location": "import",
                            "path": "./ledger-connect.types",
                            "id": "src/components/functional/ledger-connect-components/ledger-connect.types.ts::ILedgerConnectModalData"
                        }
                    }
                },
                "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-connect-modal.js.map
