import { getFeatures, getPartnerEngineConfig, isSdkUserLoaded, isUserAuthenticated, userHasPermission, } from "@boldpenguin/sdk-redux";
import { BpSdkPermissions, SdkStates } from "@boldpenguin/sdk-types";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkSetupContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, { getFeatures });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                config: getPartnerEngineConfig(state),
                expiresAt: state.auth.expiresAt,
                token: state.auth.token,
                isSdkLoaded: state.status.local.config === SdkStates.Configured,
                authenticated: isSdkUserLoaded(state) && isUserAuthenticated(state),
                hasPermission: userHasPermission(state, { permission: BpSdkPermissions.FeatureCheck }),
            };
        };
        this.checkIfReady = () => (this.isReady = this.authenticated && this.hasPermission && this.isSdkLoaded);
        this.disconnectedCallback = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
        };
        this.expiresAt = 0;
        this.token = undefined;
        this.authenticated = false;
        this.config = undefined;
        this.hasPermission = false;
        this.useExternal = false;
        this.isReady = false;
        this.isSdkLoaded = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-setup-container');
        customElements.whenDefined('bp-sdk-external-setup-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
    }
    onExpiresAtChange(expiresAt) {
        const diff = expiresAt - Date.now();
        if (diff > 0) {
            setTimeout(this.checkIfReady, diff);
        }
        this.checkIfReady();
    }
    onCheckAuthenticationChange() {
        this.checkIfReady();
    }
    onIsReadyChange(isReady) {
        if (isReady && this.getFeatures) {
            this.getFeatures({ config: this.config });
        }
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-setup-container", { config: this.config, isReady: this.isReady, isSdkLoaded: this.isSdkLoaded, token: this.token }, h("slot", null)));
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-setup-container__container" }, h("slot", null)));
    }
    static get is() { return "bp-sdk-setup-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-setup-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-setup-container.css"]
        };
    }
    static get properties() {
        return {
            "expiresAt": {
                "type": "number",
                "mutable": true,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "expires-at",
                "reflect": false,
                "defaultValue": "0"
            },
            "token": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "token",
                "reflect": false
            },
            "authenticated": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "authenticated",
                "reflect": false,
                "defaultValue": "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": ""
                }
            },
            "hasPermission": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "has-permission",
                "reflect": false,
                "defaultValue": "false"
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "isReady": {},
            "isSdkLoaded": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "expiresAt",
                "methodName": "onExpiresAtChange"
            }, {
                "propName": "authenticated",
                "methodName": "onCheckAuthenticationChange"
            }, {
                "propName": "hasPermission",
                "methodName": "onCheckAuthenticationChange"
            }, {
                "propName": "isSdkLoaded",
                "methodName": "onCheckAuthenticationChange"
            }, {
                "propName": "isReady",
                "methodName": "onIsReadyChange"
            }];
    }
}
