import { getQuoteById } from "@boldpenguin/sdk-redux";
import { CARRIER_CODES_WITH_EXPIRING_QUOTES, } from "@boldpenguin/sdk-types";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkQuoteRequestButtonContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            const quote = getQuoteById(state, this.quoteId);
            return {
                isQuoteExpired: CARRIER_CODES_WITH_EXPIRING_QUOTES.includes(quote === null || quote === void 0 ? void 0 : quote.carrier.code) && (quote === null || quote === void 0 ? void 0 : quote.expired),
                quote,
            };
        };
        this.handleAfterBindResponse = (quoteWindow) => (response) => {
            quoteWindow.location.href = response;
        };
        this.onBind = () => {
            const quoteWindow = window.open('about:blank', '_blank');
            this.quoteWindow = quoteWindow;
            const quoteId = this.quote.parentQuoteRequestId || this.quote.id;
            this.handleBind(quoteId, this.appFormId, this.handleAfterBindResponse(quoteWindow));
            this.bpAnalyticsTrack.emit({
                eventName: 'ButtonClick',
                eventPayload: {
                    label: 'Update Selection',
                    value: this.annualPrice,
                    dataValue: `${this.carrierName}_${this.annualPrice}`,
                },
            });
        };
        this.annualPrice = '0';
        this.appFormId = '';
        this.carrierName = undefined;
        this.handleBind = undefined;
        this.quoteId = '';
        this.isQuoteExpired = false;
        this.quote = undefined;
        this.useExternal = false;
        this.quoteWindow = undefined;
    }
    resetState() {
        this.quoteWindow = undefined;
        this.quote = undefined;
    }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-quote-request-button-container');
        customElements.whenDefined('bp-sdk-external-quote-request-button-container').then(() => {
            this.useExternal = true;
        });
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-quote-request-button-container", { annualPrice: this.annualPrice, appFormId: this.appFormId, carrierName: this.carrierName, handleBind: this.handleBind, quoteId: this.quoteId }));
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-quote-request-button-container__container bp-sdk-online-quote__bind bp-sdk-quote__bind" }, h("bp-sdk-quote-request-button", { onClick: !this.isQuoteExpired && this.onBind, text: "Bind", buttonDisabled: this.isQuoteExpired })));
    }
    static get is() { return "bp-sdk-quote-request-button-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-quote-request-button-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-quote-request-button-container.css"]
        };
    }
    static get properties() {
        return {
            "annualPrice": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Annual Price"
                },
                "attribute": "annual-price",
                "reflect": false,
                "defaultValue": "'0'"
            },
            "appFormId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Application form id"
                },
                "attribute": "app-form-id",
                "reflect": false,
                "defaultValue": "''"
            },
            "carrierName": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Carrier name"
                },
                "attribute": "carrier-name",
                "reflect": false
            },
            "handleBind": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "HandleBindFunction",
                    "resolved": "(quoteId: string, appFormId: string, afterResponse?: (response: string) => void) => void",
                    "references": {
                        "HandleBindFunction": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::HandleBindFunction"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "quote bind function"
                }
            },
            "quoteId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Quote id"
                },
                "attribute": "quote-id",
                "reflect": false,
                "defaultValue": "''"
            },
            "isQuoteExpired": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is quote expired"
                },
                "attribute": "is-quote-expired",
                "reflect": false,
                "defaultValue": "false"
            },
            "quote": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IQuotesState",
                    "resolved": "IQuotesState",
                    "references": {
                        "IQuotesState": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IQuotesState"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Quote object injected from redux"
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "quoteWindow": {}
        };
    }
    static get events() {
        return [{
                "method": "bpAnalyticsTrack",
                "name": "bpAnalyticsTrack",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "IAnalyticsTrackEventDetail",
                    "resolved": "IAnalyticsTrackEventDetail",
                    "references": {
                        "IAnalyticsTrackEventDetail": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IAnalyticsTrackEventDetail"
                        }
                    }
                }
            }];
    }
}
