import { cloneApplicationForm, doesUserOwnApplicationForm, getFormId, getPartnerEngineConfig, isFormCloned, isFormLoaded, isFormLocked, 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 BpSdkTakeOverContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, {
                cloneApplicationForm,
            });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                appFormId: getFormId(state),
                isLocked: isFormLocked(state),
                isCloned: isFormCloned(state),
                canTakeOver: userHasPermission(state, { permission: BpSdkPermissions.TakeOver }),
                isOwner: doesUserOwnApplicationForm(state),
                isFormLoaded: isFormLoaded(state),
                config: getPartnerEngineConfig(state),
            };
        };
        this.takeOverHandler = () => {
            this.cloneApplicationForm({
                appFormId: this.appFormId,
                config: this.config,
            });
        };
        this.isLocked = undefined;
        this.isCloned = undefined;
        this.canTakeOver = undefined;
        this.isOwner = undefined;
        this.isFormLoaded = undefined;
        this.appFormId = undefined;
        this.config = undefined;
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-take-over-container');
        customElements.whenDefined('bp-sdk-external-take-over-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-take-over-container", { appFormId: this.appFormId, canTakeOver: this.canTakeOver, config: this.config, isCloned: this.isCloned, isLocked: this.isLocked, isOwner: this.isOwner, isFormLoaded: this.isFormLoaded, takeOverHandler: this.takeOverHandler }));
    }
    renderInternal() {
        return (h("bp-sdk-take-over", { canTakeOver: this.canTakeOver, isCloned: this.isCloned, isLocked: this.isLocked, isOwner: this.isOwner, isFormLoaded: this.isFormLoaded, takeOverHandler: this.takeOverHandler }));
    }
    static get is() { return "bp-sdk-take-over-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-take-over-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-take-over-container.css"]
        };
    }
    static get properties() {
        return {
            "isLocked": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is locked injected from redux"
                },
                "attribute": "is-locked",
                "reflect": false
            },
            "isCloned": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is clocked injected from redux"
                },
                "attribute": "is-cloned",
                "reflect": false
            },
            "canTakeOver": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Can take over injected from redux"
                },
                "attribute": "can-take-over",
                "reflect": false
            },
            "isOwner": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is owner injected from redux"
                },
                "attribute": "is-owner",
                "reflect": false
            },
            "isFormLoaded": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is application form loaded injected from redux"
                },
                "attribute": "is-form-loaded",
                "reflect": false
            },
            "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
            },
            "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"
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {}
        };
    }
    static get elementRef() { return "el"; }
}
