import { h, forceUpdate } from "@stencil/core";
import { EventBus } from "../../utils/EventBus";
import { WalletConnectEventsEnum } from "./wallet-connect-modal.types";
import QRCode from "qrcode";
import { DataTestIdsEnum } from "../../constants/dataTestIds.enum";
export class WalletConnectModal {
    constructor() {
        this.eventBus = new EventBus();
        this.data = {
            wcURI: '',
        };
        this.qrCodeSvg = '';
    }
    async getEventBus() {
        return this.eventBus;
    }
    async generateSVG() {
        try {
            const svg = await QRCode.toString(this.data.wcURI, {
                type: 'svg',
            });
            this.qrCodeSvg = svg;
            return svg;
        }
        catch (error) {
            console.error('Error generating QR Code:', error);
            return '';
        }
    }
    onDataChange(data) {
        if (data.wcURI) {
            this.generateSVG();
        }
    }
    render() {
        return (h("generic-modal", { key: 'ddbeae88f27162ecbf54620e8e6d3dc8b3e933e7', modalTitle: h("div", { "data-testid": DataTestIdsEnum.walletConnetModalTitle }, "xPortal Mobile Wallet"), modalSubtitle: h("div", { "data-testid": DataTestIdsEnum.walletConnetModalSubtitle }, "Scan this QR code with your app"), onClose: () => this.close(), body: h("div", { class: "modal-body" }, this.qrCodeSvg ? (h("div", { class: "qr-code-container", "data-testid": DataTestIdsEnum.walletConnectQrCode, innerHTML: this.qrCodeSvg })) : (h("generic-spinner", { "data-testid": DataTestIdsEnum.walletConnectLoading }))) }));
    }
    close(props = { isUserClick: true }) {
        if (props.isUserClick) {
            this.eventBus.publish(WalletConnectEventsEnum.CLOSE);
        }
        if (this.hostElement && this.hostElement.parentNode) {
            this.hostElement.parentNode.removeChild(this.hostElement);
        }
    }
    dataUpdate(payload) {
        if (payload.shouldClose) {
            return this.close({ isUserClick: false });
        }
        this.data = Object.assign({}, payload);
        forceUpdate(this);
    }
    componentDidLoad() {
        this.eventBus.subscribe(WalletConnectEventsEnum.DATA_UPDATE, this.dataUpdate.bind(this));
    }
    disconnectedCallback() {
        this.eventBus.unsubscribe(WalletConnectEventsEnum.DATA_UPDATE, this.dataUpdate.bind(this));
    }
    static get is() { return "wallet-connect-modal"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["wallet-connect-modal.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["wallet-connect-modal.css"]
        };
    }
    static get properties() {
        return {
            "data": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IWalletConnectModalData",
                    "resolved": "IWalletConnectModalData",
                    "references": {
                        "IWalletConnectModalData": {
                            "location": "import",
                            "path": "./wallet-connect-modal.types",
                            "id": "src/components/wallet-connect-modal/wallet-connect-modal.types.ts::IWalletConnectModalData"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "defaultValue": "{\n    wcURI: '',\n  }"
            }
        };
    }
    static get states() {
        return {
            "qrCodeSvg": {}
        };
    }
    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"; }
    static get watchers() {
        return [{
                "propName": "data",
                "methodName": "onDataChange"
            }];
    }
}
//# sourceMappingURL=wallet-connect-modal.js.map
