import { getAuthConfig } from "@boldpenguin/sdk-auth";
import { getPartnerEngineConfig, loadGuestApplicationForm } from "@boldpenguin/sdk-redux";
import { Storefront } from "@boldpenguin/sdk-requests";
import { ApplicationSource, } from "@boldpenguin/sdk-types";
import { getCoreOptions } from "@boldpenguin/sdk-utility";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkCreateAppFormContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, {
                loadGuestApplicationForm,
            });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            const peConfig = getPartnerEngineConfig(state);
            return {
                peConfig: peConfig,
                authConfig: getAuthConfig(getCoreOptions({ env: peConfig.env })),
            };
        };
        this.createApp = () => {
            this.buttonDisabled = true;
            Storefront.getStorefrontByCode(this.storefrontCode, this.authConfig)
                .then((storefront) => storefront.parsedBody)
                .then((sf) => {
                this.loadGuestApplicationForm({
                    tenantId: sf.tenant_id,
                    env: this.peConfig.env,
                    uri: this.peConfig.uri,
                    applicationSource: ApplicationSource.Storefront,
                    applicationMetadata: {
                        storefront_code: sf.code,
                        storefront_name: sf.agency_name,
                    },
                });
            })
                .then(() => (this.buttonDisabled = false));
        };
        this.peConfig = undefined;
        this.authConfig = undefined;
        this.storefrontCode = undefined;
        this.text = undefined;
        this.buttonDisabled = false;
        this.useExternal = false;
        this.storeFront = undefined;
    }
    resetState() {
        this.buttonDisabled = false;
        this.handleSubscriptions();
    }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-create-app-form-container');
        customElements.whenDefined('bp-sdk-external-create-app-form-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-create-app-form-container", { peConfig: this.peConfig, authConfig: this.authConfig, createAppFormHandler: this.createApp, storefrontCode: this.storefrontCode, text: this.text }));
    }
    renderInternal() {
        return h("bp-sdk-create-app-form", { buttonDisabled: this.buttonDisabled, createAppFormHandler: this.createApp, text: this.text });
    }
    static get is() { return "bp-sdk-create-app-form-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-create-app-form-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-create-app-form-container.css"]
        };
    }
    static get properties() {
        return {
            "peConfig": {
                "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": "PartnerEngine Config injected from redux"
                }
            },
            "authConfig": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IAuthenticatorConfig",
                    "resolved": "IAuthenticatorConfig",
                    "references": {
                        "IAuthenticatorConfig": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IAuthenticatorConfig"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Authenticator config injected from redux"
                }
            },
            "storefrontCode": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Storefront code"
                },
                "attribute": "storefront-code",
                "reflect": false
            },
            "text": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Button Text"
                },
                "attribute": "text",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "buttonDisabled": {},
            "useExternal": {},
            "storeFront": {}
        };
    }
}
