import { forceUpdate, h } from "@stencil/core";
import { EventBus } from "../../../utils/EventBus";
import { SignEventsEnum } from "./sign-transactions-modal.types";
import state, { resetState } from "./signTransactionsModalStore";
const signScreens = {
    FungibleESDT: 'token-component',
    SemiFungibleESDT: 'fungible-component',
    NonFungibleESDT: 'fungible-component',
    MetaESDT: 'token-component',
};
export class SignTransactionsModal {
    constructor() {
        this.eventBus = new EventBus();
        this.data = {
            commonData: {
                egldLabel: '',
                feeLimit: '',
                feeInFiatLimit: '',
                transactionsCount: 0,
                currentIndex: 0,
                ppuOptions: [],
            },
            tokenTransaction: null,
            nftTransaction: null,
            sftTransaction: null,
        };
    }
    async getEventBus() {
        return this.eventBus;
    }
    onDataChange(data) {
        for (const key in data) {
            if (Object.prototype.hasOwnProperty.call(state, key)) {
                state[key] = data[key];
            }
        }
        state.isWaitingForSignature = false;
        state.isLoading = false;
    }
    componentWillLoad() {
        state.onConfirm = () => {
            this.eventBus.publish(SignEventsEnum.CONFIRM);
        };
        state.onCancel = () => {
            this.eventBus.publish(SignEventsEnum.CLOSE);
        };
        state.onBack = () => {
            this.eventBus.publish(SignEventsEnum.BACK);
        };
        state.onSetPpu = (ppu) => {
            this.eventBus.publish(SignEventsEnum.SET_PPU, ppu);
        };
    }
    render() {
        const { commonData, isLoading } = state;
        const { tokenType, currentIndex, transactionsCount } = commonData;
        const SignScreen = signScreens[tokenType];
        return (h("generic-modal", { key: 'd40e2e6b2de1b0c5321c8e155d78dbe99f96b613', onClose: () => this.close(), modalTitle: "Sign transaction", modalSubtitle: `Transaction ${currentIndex + 1} of ${transactionsCount}`, body: isLoading ? (h("div", { class: "loading-spinner" }, h("generic-spinner", null))) : (h(SignScreen, null)) }));
    }
    close(props = { isUserClick: true }) {
        resetState();
        if (props.isUserClick) {
            this.eventBus.publish(SignEventsEnum.CLOSE);
        }
        if (this.hostElement && this.hostElement.parentNode) {
            this.hostElement.parentNode.removeChild(this.hostElement);
        }
    }
    dataUpdate(payload) {
        this.data = Object.assign({}, payload);
        forceUpdate(this);
    }
    componentDidLoad() {
        this.eventBus.subscribe(SignEventsEnum.DATA_UPDATE, this.dataUpdate.bind(this));
    }
    disconnectedCallback() {
        resetState();
        this.eventBus.unsubscribe(SignEventsEnum.DATA_UPDATE, this.dataUpdate.bind(this));
    }
    static get is() { return "sign-transactions-modal"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["sign-transactions-modal.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["sign-transactions-modal.css"]
        };
    }
    static get properties() {
        return {
            "data": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "ISignTransactionsModalData",
                    "resolved": "ISignTransactionsModalData",
                    "references": {
                        "ISignTransactionsModalData": {
                            "location": "import",
                            "path": "./sign-transactions-modal.types",
                            "id": "src/components/functional/sign-transactions-modal/sign-transactions-modal.types.ts::ISignTransactionsModalData"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "defaultValue": "{\n    commonData: {\n      egldLabel: '',\n      feeLimit: '',\n      feeInFiatLimit: '',\n      transactionsCount: 0,\n      currentIndex: 0,\n      ppuOptions: [],\n    },\n    tokenTransaction: null,\n    nftTransaction: null,\n    sftTransaction: null,\n  }"
            }
        };
    }
    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=sign-transactions-modal.js.map
