import { createQuestionSet, doesUserOwnApplicationForm, getFormId, getPartnerEngineConfig, getQuestionSetByCode, userHasPermission, } from "@boldpenguin/sdk-redux";
import { BpSdkPermissions, } from "@boldpenguin/sdk-types";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkSendToExchangeContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, {
                createQuestionSet,
            });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => ({
            config: getPartnerEngineConfig(state),
            appFormId: getFormId(state),
            isCurrentOwner: doesUserOwnApplicationForm(state),
            canSendToExchange: userHasPermission(state, { permission: BpSdkPermissions.SendToExchange }),
            hasSentToExchange: !!getQuestionSetByCode(state, 'send_to_bold_penguin'),
        });
        this.sendToExchange = () => {
            this.createQuestionSet({
                config: this.config,
                appFormQuestionSet: {
                    application_form_id: this.appFormId,
                    question_set: {
                        code: 'send_to_bold_penguin',
                    },
                },
            });
        };
        this.isCurrentOwner = undefined;
        this.canSendToExchange = undefined;
        this.hasSentToExchange = undefined;
        this.config = undefined;
        this.appFormId = undefined;
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-send-to-exchange-container');
        customElements.whenDefined('bp-sdk-external-send-to-exchange-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-send-to-exchange-container", { appFormId: this.appFormId, hasSentToExchange: this.hasSentToExchange, config: this.config, sendToExchangeHandler: this.sendToExchange, canSendToExchange: this.canSendToExchange, isCurrentOwner: this.isCurrentOwner }));
    }
    renderInternal() {
        return (this.canSendToExchange &&
            this.isCurrentOwner && (h("div", { class: "bp-sdk-quote__send-to-exchange" }, h("bp-sdk-send-to-exchange", { buttonDisabled: this.hasSentToExchange, sendToExchangeHandler: this.sendToExchange }))));
    }
    static get is() { return "bp-sdk-send-to-exchange-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-send-to-exchange-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-send-to-exchange-container.css"]
        };
    }
    static get properties() {
        return {
            "isCurrentOwner": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is current ower injected from redux"
                },
                "attribute": "is-current-owner",
                "reflect": false
            },
            "canSendToExchange": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Can send to exchange injected from redux"
                },
                "attribute": "can-send-to-exchange",
                "reflect": false
            },
            "hasSentToExchange": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Has sent to exchange injected from redux"
                },
                "attribute": "has-sent-to-exchange",
                "reflect": false
            },
            "config": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IPartnerEngineConfig",
                    "resolved": "IPartnerEngineConfig",
                    "references": {
                        "IPartnerEngineConfig": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IPartnerEngineConfig"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Partner engine config injected from redux"
                }
            },
            "appFormId": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "App Form ID injected from redux"
                },
                "attribute": "app-form-id",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "useExternal": {}
        };
    }
    static get elementRef() { return "el"; }
}
