import { forceUpdate, h } from "@stencil/core";
import { EventBus } from "../../../utils/EventBus";
import { SignEventsEnum } from "./sign-transactions-panel.types";
import state, { resetState } from "./signTransactionsPanelStore";
const signScreens = {
    FungibleESDT: 'token-component',
    SemiFungibleESDT: 'fungible-component',
    NonFungibleESDT: 'fungible-component',
    MetaESDT: 'token-component',
};
export class SignTransactionsPanel {
    constructor() {
        this.eventBus = new EventBus();
        this.isOpen = false;
        this.data = {
            commonData: {
                egldLabel: '',
                feeLimit: '',
                feeInFiatLimit: '',
                transactionsCount: 0,
                currentIndex: 0,
                ppuOptions: [],
            },
            tokenTransaction: null,
            nftTransaction: null,
            sftTransaction: null,
        };
    }
    componentWillLoad() {
        state.onCancel = () => {
            this.onClose({ isUserClick: true });
        };
        state.onConfirm = () => {
            this.eventBus.publish(SignEventsEnum.CONFIRM);
        };
        state.onBack = () => {
            this.eventBus.publish(SignEventsEnum.BACK);
        };
        state.onSetPpu = (ppu) => {
            this.eventBus.publish(SignEventsEnum.SET_PPU, ppu);
        };
    }
    componentDidLoad() {
        this.eventBus.subscribe(SignEventsEnum.DATA_UPDATE, this.dataUpdate.bind(this));
        this.eventBus.subscribe(SignEventsEnum.OPEN_SIGN_TRANSACTIONS_PANEL, this.handleOpen.bind(this));
        this.eventBus.subscribe(SignEventsEnum.CLOSE_SIGN_TRANSACTIONS_PANEL, this.onClose.bind(this, { isUserClick: false }));
    }
    disconnectedCallback() {
        resetState();
        this.eventBus.unsubscribe(SignEventsEnum.DATA_UPDATE, this.dataUpdate.bind(this));
        this.eventBus.unsubscribe(SignEventsEnum.OPEN_SIGN_TRANSACTIONS_PANEL, this.handleOpen.bind(this));
        this.eventBus.unsubscribe(SignEventsEnum.CLOSE_SIGN_TRANSACTIONS_PANEL, this.onClose.bind(this, { isUserClick: false }));
    }
    handleOpen() {
        this.isOpen = true;
    }
    handleClose() {
        this.isOpen = false;
        this.onClose({ isUserClick: true });
    }
    onClose(props = { isUserClick: true }) {
        this.isOpen = false;
        resetState();
        if (props.isUserClick) {
            this.eventBus.publish(SignEventsEnum.CLOSE_SIGN_TRANSACTIONS_PANEL);
        }
    }
    dataUpdate(payload) {
        this.data = Object.assign({}, payload);
        forceUpdate(this);
    }
    onDataChange(data) {
        for (const key in data) {
            if (Object.prototype.hasOwnProperty.call(state, key)) {
                state[key] = data[key];
            }
        }
        state.isWaitingForSignature = false;
        state.isLoading = false;
    }
    async getEventBus() {
        return this.eventBus;
    }
    render() {
        const { commonData, isLoading } = state;
        const { tokenType, currentIndex, transactionsCount } = commonData;
        const SignScreen = signScreens[tokenType];
        return (h("mvx-side-panel", { key: 'f5b19ad06601b8b8132db8c89201c57a84705f1b', isOpen: this.isOpen, panelClassName: "sign-transactions-panel", onClose: this.handleClose.bind(this) }, h("div", { key: '78d8c0e08c4cac94918b21bd5ee2ba657893045b', class: "sign-transactions-content" }, h("div", { key: '582075c236994a1e80285e7a5242619af15b5085', class: "sign-transactions-header" }, h("h2", { key: 'e5bd15e0d32067f8c64c2b852f2ac490d1242752' }, "Sign transaction"), h("h4", { key: '070cfa235bec8152978c34899fb5665dea1565d7' }, "Transaction ", currentIndex + 1, " of ", transactionsCount)), h("div", { key: '70ef74abb16a5f97640e9a2ccce162135311dd79', class: "sign-transactions-body" }, isLoading ? (h("div", { class: "loading-spinner" }, h("mvx-generic-spinner", null))) : (h(SignScreen, null))))));
    }
    static get is() { return "mvx-sign-transactions-panel"; }
    static get originalStyleUrls() {
        return {
            "$": ["sign-transactions-panel.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["sign-transactions-panel.css"]
        };
    }
    static get properties() {
        return {
            "data": {
                "type": "unknown",
                "attribute": "data",
                "mutable": false,
                "complexType": {
                    "original": "ISignTransactionsPanelData",
                    "resolved": "ISignTransactionsPanelData",
                    "references": {
                        "ISignTransactionsPanelData": {
                            "location": "import",
                            "path": "./sign-transactions-panel.types",
                            "id": "src/components/functional/sign-transactions-panel/sign-transactions-panel.types.ts::ISignTransactionsPanelData"
                        }
                    }
                },
                "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 states() {
        return {
            "isOpen": {}
        };
    }
    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 watchers() {
        return [{
                "propName": "data",
                "methodName": "onDataChange"
            }];
    }
}
//# sourceMappingURL=sign-transactions-panel.js.map
