import { updateActiveAnswerId, updateErrors } from "@boldpenguin/sdk-redux";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkDynamicInputReduxContainer {
    constructor() {
        this.handleUpdateActiveAnswerId = (answerId) => {
            this.updateActiveAnswerId(answerId);
        };
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, {
                updateActiveAnswerId,
                updateErrors,
            });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                errorState: state.application.errors,
                activeId: state.application.questionSets.activeAnswerId,
            };
        };
        this.answer = undefined;
        this.label = undefined;
        this.labelledBy = undefined;
        this.useSelectAlias = false;
        this.showBusinessClassificationSelector = true;
        this.shouldHideLabel = false;
        this.errorState = {};
        this.activeId = '';
        this.isAccumulatorAnswer = false;
        this.disableAddressAutocomplete = false;
        this.updateAccumulatorErrors = undefined;
        this.code = undefined;
        this.errors = [];
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-dynamic-input-redux-container');
        customElements.whenDefined('bp-sdk-external-dynamic-input-redux-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
        this.onErrorsStateChange(this.errorState);
        this.onAnswerChange(this.answer);
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    onErrorsStateChange(errorState) {
        this.getErrors(this.answer, errorState);
        this.errorsUpdate.emit(this.errors);
    }
    onAnswerChange(answer) {
        this.getErrors(answer, this.errorState);
    }
    getErrors(answer, errorsState) {
        var _a;
        this.errors = (_a = errorsState[answer === null || answer === void 0 ? void 0 : answer.id]) !== null && _a !== void 0 ? _a : [];
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-dynamic-input-redux-container", { activeId: this.activeId, answer: this.answer, errors: this.errors, errorState: this.errorState, isAccumulatorAnswer: this.isAccumulatorAnswer, label: this.label, labelledBy: this.labelledBy, disableAddressAutocomplete: this.disableAddressAutocomplete, shouldHideLabel: this.shouldHideLabel, showBusinessClassificationSelector: this.showBusinessClassificationSelector, updateActiveAnswerId: this.handleUpdateActiveAnswerId, updateAccumulatorErrors: this.updateAccumulatorErrors, updateErrors: this.updateErrors, useSelectAlias: this.useSelectAlias, code: this.code }));
    }
    renderInternal() {
        return (h("bp-sdk-dynamic-input-event-container", { answer: this.answer, errors: this.errors, label: this.label, labelledBy: this.labelledBy, shouldHideLabel: this.shouldHideLabel, disableAddressAutocomplete: this.disableAddressAutocomplete, showBusinessClassificationSelector: this.showBusinessClassificationSelector, updateActiveAnswerId: this.updateActiveAnswerId, updateErrors: this.updateErrors, useSelectAlias: this.useSelectAlias }));
    }
    static get is() { return "bp-sdk-dynamic-input-redux-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["bp-sdk-dynamic-input-redux-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-dynamic-input-redux-container.css"]
        };
    }
    static get properties() {
        return {
            "answer": {
                "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": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Application form answer"
                }
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Label to use"
                },
                "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
            },
            "useSelectAlias": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Use alias for select"
                },
                "attribute": "use-select-alias",
                "reflect": false,
                "defaultValue": "false"
            },
            "showBusinessClassificationSelector": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Show business class selector"
                },
                "attribute": "show-business-classification-selector",
                "reflect": false,
                "defaultValue": "true"
            },
            "shouldHideLabel": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Should hide label"
                },
                "attribute": "should-hide-label",
                "reflect": false,
                "defaultValue": "false"
            },
            "errorState": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IErrorState",
                    "resolved": "IErrorState",
                    "references": {
                        "IErrorState": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IErrorState"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Errors injected from redux"
                },
                "defaultValue": "{}"
            },
            "activeId": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Active question set ID injected from redux"
                },
                "attribute": "active-id",
                "reflect": false,
                "defaultValue": "''"
            },
            "isAccumulatorAnswer": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Show if Answer is accumulator"
                },
                "attribute": "is-accumulator-answer",
                "reflect": false,
                "defaultValue": "false"
            },
            "disableAddressAutocomplete": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Disables Google Autocomplete for address groups when true"
                },
                "attribute": "disable-address-autocomplete",
                "reflect": false,
                "defaultValue": "false"
            },
            "updateAccumulatorErrors": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "() => void",
                    "resolved": "() => void",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Callback for accumulator Error"
                }
            },
            "code": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | undefined",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question code to allow targeting specific inputs by code in css"
                },
                "attribute": "code",
                "reflect": true
            }
        };
    }
    static get states() {
        return {
            "errors": {},
            "useExternal": {}
        };
    }
    static get events() {
        return [{
                "method": "errorsUpdate",
                "name": "errorsUpdate",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "string[]",
                    "resolved": "string[]",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "errorState",
                "methodName": "onErrorsStateChange"
            }, {
                "propName": "answer",
                "methodName": "onAnswerChange"
            }];
    }
}
