import { initializeAddressValidation, selectAddressValidationOptions, selectHasAddressCompletedValidation, selectIsAddressGroupValidationInitialized, selectIsAddressValidationEnabled, setAddressValidationStatus, selectIsActiveAnswerIdInGroup, selectHasValidationOccurredForAddress, resetAddressValidation, isFeatureEnabled, } from "@boldpenguin/sdk-redux";
import { QuestionGroupType, ValidationStatus, } from "@boldpenguin/sdk-types";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
import { hasAnswerChanged } from "@boldpenguin/sdk-utility";
export class BpSdkAddressGroupContainer {
    constructor() {
        this.mapStateToProps = (state) => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
            return {
                enableAddressValidation: selectIsAddressValidationEnabled(state),
                addressValidationOptions: selectAddressValidationOptions(state),
                isValidationComplete: selectHasAddressCompletedValidation(state, (_b = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.poolId) !== null && _b !== void 0 ? _b : (_c = this.questionGroup) === null || _c === void 0 ? void 0 : _c.groupId),
                hasValidationOccurred: selectHasValidationOccurredForAddress(state, (_e = (_d = this.questionGroup) === null || _d === void 0 ? void 0 : _d.poolId) !== null && _e !== void 0 ? _e : (_f = this.questionGroup) === null || _f === void 0 ? void 0 : _f.groupId),
                isAddressGroupValidationInitialized: selectIsAddressGroupValidationInitialized(state, (_h = (_g = this.questionGroup) === null || _g === void 0 ? void 0 : _g.poolId) !== null && _h !== void 0 ? _h : (_j = this.questionGroup) === null || _j === void 0 ? void 0 : _j.groupId),
                isActiveAnswerInAddressGroup: selectIsActiveAnswerIdInGroup(state, (_l = (_k = this.questionGroup) === null || _k === void 0 ? void 0 : _k.poolId) !== null && _l !== void 0 ? _l : (_m = this.questionGroup) === null || _m === void 0 ? void 0 : _m.groupId),
                isAddressValidationFeatureEnabled: isFeatureEnabled(state, 'enable_address_validation'),
            };
        };
        this.questionGroup = undefined;
        this.answers = [];
        this.label = undefined;
        this.labelledBy = undefined;
        this.enableAddressValidation = false;
        this.addressValidationOptions = undefined;
        this.isValidationComplete = undefined;
        this.isAddressGroupValidationInitialized = undefined;
        this.isActiveAnswerInAddressGroup = undefined;
        this.hasValidationOccurred = undefined;
        this.isAddressValidationFeatureEnabled = undefined;
        this.useExternal = false;
        this.useAddressValidation = false;
    }
    onAddressValidationRequirementChange() {
        this.updateUseAddressValidation(this.questionGroup, this.enableAddressValidation, this.addressValidationOptions, this.isValidationComplete);
    }
    onSaveAnswer(event) {
        var _a, _b;
        if (this.hasValidationOccurred && this.isActiveAnswerInAddressGroup && hasAnswerChanged(event.detail.answer, event.detail.new_value)) {
            this.setAddressValidationStatus({
                validationStatus: ValidationStatus.REQUIRED,
                addressValidationId: (_a = this.questionGroup.poolId) !== null && _a !== void 0 ? _a : this.questionGroup.groupId,
            });
            this.resetAddressValidation((_b = this.questionGroup.poolId) !== null && _b !== void 0 ? _b : this.questionGroup.groupId);
        }
    }
    onShouldInitialization() {
        var _a;
        if (this.useAddressValidation && this.isAddressGroupValidationInitialized === false) {
            this.initializeAddressValidation((_a = this.questionGroup.poolId) !== null && _a !== void 0 ? _a : this.questionGroup.groupId);
        }
    }
    handleSubscriptions() {
        if (this.storeUnsubscribe) {
            this.storeUnsubscribe();
        }
        store.mapDispatchToProps(this, {
            initializeAddressValidation,
            setAddressValidationStatus,
            resetAddressValidation,
        });
        store.mapStateToProps(this, this.mapStateToProps);
    }
    resetState() {
        this.enableAddressValidation = false;
        this.useAddressValidation = false;
    }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-address-group-container');
        customElements.whenDefined('bp-sdk-external-address-group-container').then(() => {
            this.useExternal = true;
        });
    }
    updateUseAddressValidation(questionGroup, enableAddressValidation, addressValidationOptions, isValidationComplete) {
        this.useAddressValidation = Boolean(enableAddressValidation &&
            !isValidationComplete &&
            this.isAddressValidationFeatureEnabled &&
            questionGroup &&
            (questionGroup.group_type === QuestionGroupType.ValidatedAddress ||
                // backward compat only, remove with content update to new group type
                (addressValidationOptions &&
                    this.shouldQuestionGroupValidateAddress(questionGroup.parentQuestionAnswer.question.code, addressValidationOptions))));
    }
    shouldQuestionGroupValidateAddress(parentQuestionCode, addressValidationOptions) {
        return (addressValidationOptions.includePartialQuestionCodes.some((includedPartial) => parentQuestionCode.includes(includedPartial)) &&
            !addressValidationOptions.excludePartialQuestionCodes.some((excludedPartial) => parentQuestionCode.includes(excludedPartial)));
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        var _a, _b, _c;
        return (h("bp-sdk-external-address-group", { answers: this.answers, label: this.label, labelledBy: this.labelledBy, useAddressValidation: this.useAddressValidation, addressValidationId: (_b = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.poolId) !== null && _b !== void 0 ? _b : (_c = this.questionGroup) === null || _c === void 0 ? void 0 : _c.groupId }));
    }
    renderInternal() {
        var _a, _b, _c;
        return (h("bp-sdk-address-group", { answers: this.answers, label: this.label, labelledBy: this.labelledBy, useAddressValidation: this.useAddressValidation, addressValidationId: (_b = (_a = this.questionGroup) === null || _a === void 0 ? void 0 : _a.poolId) !== null && _b !== void 0 ? _b : (_c = this.questionGroup) === null || _c === void 0 ? void 0 : _c.groupId }));
    }
    static get is() { return "bp-sdk-address-group-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["bp-sdk-address-group-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-address-group-container.css"]
        };
    }
    static get properties() {
        return {
            "questionGroup": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IMappedQuestionGroupAnswers | undefined",
                    "resolved": "IMappedQuestionGroupAnswers",
                    "references": {
                        "IMappedQuestionGroupAnswers": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IMappedQuestionGroupAnswers"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            },
            "answers": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IAnswer[]",
                    "resolved": "IAnswer[]",
                    "references": {
                        "IAnswer": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IAnswer"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "group of answers"
                },
                "defaultValue": "[]"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "label"
                },
                "attribute": "label",
                "reflect": false
            },
            "labelledBy": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Labels that describe group"
                },
                "attribute": "labelled-by",
                "reflect": false
            },
            "enableAddressValidation": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Enables Google Autocomplete for address groups when true, injected by redux"
                },
                "attribute": "enable-address-validation",
                "reflect": false,
                "defaultValue": "false"
            },
            "addressValidationOptions": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IAddressValidationOptions | undefined",
                    "resolved": "IAddressValidationOptions",
                    "references": {
                        "IAddressValidationOptions": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IAddressValidationOptions"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Partial question codes of parent questions for address validation, injected by redux"
                }
            },
            "isValidationComplete": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is true when address has already completed validation"
                },
                "attribute": "is-validation-complete",
                "reflect": false
            },
            "isAddressGroupValidationInitialized": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean | null",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is true when address group has been initialized for validation"
                },
                "attribute": "is-address-group-validation-initialized",
                "reflect": false
            },
            "isActiveAnswerInAddressGroup": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is true when active answer id is an answer within the address question group"
                },
                "attribute": "is-active-answer-in-address-group",
                "reflect": false
            },
            "hasValidationOccurred": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is true when validation has been run for the address group"
                },
                "attribute": "has-validation-occurred",
                "reflect": false
            },
            "isAddressValidationFeatureEnabled": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is true when address validation feature flag is enabled, injected by redux"
                },
                "attribute": "is-address-validation-feature-enabled",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "useAddressValidation": {}
        };
    }
    static get watchers() {
        return [{
                "propName": "addressValidationOptions",
                "methodName": "onAddressValidationRequirementChange"
            }, {
                "propName": "questionGroup",
                "methodName": "onAddressValidationRequirementChange"
            }, {
                "propName": "enableAddressValidation",
                "methodName": "onAddressValidationRequirementChange"
            }, {
                "propName": "isValidationComplete",
                "methodName": "onAddressValidationRequirementChange"
            }, {
                "propName": "isAddressValidationFeatureEnabled",
                "methodName": "onAddressValidationRequirementChange"
            }, {
                "propName": "useAddressValidation",
                "methodName": "onShouldInitialization"
            }, {
                "propName": "isAddressGroupValidationInitialized",
                "methodName": "onShouldInitialization"
            }];
    }
    static get listeners() {
        return [{
                "name": "saveAnswer",
                "method": "onSaveAnswer",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
