import { isAutocompleteEnabled, removeActiveAnswerId } from "@boldpenguin/sdk-redux";
import { getAutocomplete } from "@boldpenguin/sdk-utility";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkGooglePlacesAutocompleteContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            store.mapDispatchToProps(this, { removeActiveAnswerId });
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                googleApiToken: state.config.googleApiToken,
                isAutocompleteEnabled: isAutocompleteEnabled(state),
            };
        };
        this.answerId = undefined;
        this.code = undefined;
        this.labelId = undefined;
        this.placeholder = '';
        this.filter = undefined;
        this.label = undefined;
        this.labelledBy = undefined;
        this.required = undefined;
        this.errors = [];
        this.errorText = undefined;
        this.touched = false;
        this.pristine = true;
        this.isCompleted = false;
        this.isReadOnly = false;
        this.questionId = undefined;
        this.fieldType = undefined;
        this.min = -1;
        this.max = -1;
        this.referenceType = undefined;
        this.helpText = undefined;
        this.googleApiToken = undefined;
        this.isAutocompleteEnabled = undefined;
        this.answeredBySource = undefined;
        this.autoComplete = undefined;
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-google-places-autocomplete-container');
        customElements.whenDefined('bp-sdk-external-google-places-autocomplete-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code: this.code,
            fieldType: this.fieldType,
            isAutocompleteEnabled: this.isAutocompleteEnabled,
        });
    }
    onPlacesUpdate() {
        if (!this.useExternal) {
            this.removeActiveAnswerId();
        }
    }
    onReferenceTypeChange(referenceType) {
        this.updateAutocomplete({
            referenceType,
            code: this.code,
            fieldType: this.fieldType,
            isAutocompleteEnabled: this.isAutocompleteEnabled,
        });
    }
    onCodeChange(code) {
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code,
            fieldType: this.fieldType,
            isAutocompleteEnabled: this.isAutocompleteEnabled,
        });
    }
    onFieldTypeChange(fieldType) {
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code: this.code,
            fieldType,
            isAutocompleteEnabled: this.isAutocompleteEnabled,
        });
    }
    onIsAutocompleteEnabledChange(isAutocompleteEnabled) {
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code: this.code,
            fieldType: this.fieldType,
            isAutocompleteEnabled,
        });
    }
    updateAutocomplete({ code, fieldType, referenceType, isAutocompleteEnabled, }) {
        this.autoComplete = isAutocompleteEnabled ? getAutocomplete({ referenceType, code, fieldType }) : undefined;
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-google-places-autocomplete-container", { answeredBySource: this.answeredBySource, answerId: this.answerId, code: this.code, errors: this.errors, errorText: this.errorText, fieldType: this.fieldType, helpText: this.helpText, isCompleted: this.isCompleted, isReadOnly: this.isReadOnly, filter: this.filter, googleApiToken: this.googleApiToken, labelId: this.labelId, label: this.label, labelledBy: this.labelledBy, max: this.max, min: this.min, placeholder: this.placeholder, pristine: this.pristine, questionId: this.questionId, referenceType: this.referenceType, required: this.required, touched: this.touched, isAutocompleteEnabled: this.isAutocompleteEnabled }));
    }
    renderInternal() {
        return (h("bp-sdk-google-places-autocomplete", { answeredBySource: this.answeredBySource, answerId: this.answerId, autoComplete: this.autoComplete, code: this.code, errors: this.errors, errorText: this.errorText, fieldType: this.fieldType, filter: this.filter, googleApiKey: this.googleApiToken, helpText: this.helpText, isCompleted: this.isCompleted, isReadOnly: this.isReadOnly, label: this.label, labelId: this.labelId, labelledBy: this.labelledBy, max: this.max, min: this.min, placeholder: this.placeholder, questionId: this.questionId, required: this.required, pristine: this.pristine, touched: this.touched }));
    }
    static get is() { return "bp-sdk-google-places-autocomplete-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["bp-sdk-google-places-autocomplete-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-google-places-autocomplete-container.css"]
        };
    }
    static get properties() {
        return {
            "answerId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Answer id"
                },
                "attribute": "answer-id",
                "reflect": false
            },
            "code": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question code"
                },
                "attribute": "code",
                "reflect": false
            },
            "labelId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Aria label by id"
                },
                "attribute": "label-id",
                "reflect": false
            },
            "placeholder": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "placeholder"
                },
                "attribute": "placeholder",
                "reflect": false,
                "defaultValue": "''"
            },
            "filter": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Filter text"
                },
                "attribute": "filter",
                "reflect": false
            },
            "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
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is required"
                },
                "attribute": "required",
                "reflect": false
            },
            "errors": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "string[]",
                    "resolved": "string[]",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error messages"
                },
                "defaultValue": "[]"
            },
            "errorText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error text"
                },
                "attribute": "error_text",
                "reflect": false
            },
            "touched": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "is touched"
                },
                "attribute": "touched",
                "reflect": false,
                "defaultValue": "false"
            },
            "pristine": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "is pristine"
                },
                "attribute": "pristine",
                "reflect": false,
                "defaultValue": "true"
            },
            "isCompleted": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is answer in complete state"
                },
                "attribute": "is_completed",
                "reflect": false,
                "defaultValue": "false"
            },
            "isReadOnly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "When true, the field will be readonly. It will display only the label and value as plain text."
                },
                "attribute": "is-read-only",
                "reflect": false,
                "defaultValue": "false"
            },
            "questionId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question id"
                },
                "attribute": "question_id",
                "reflect": false
            },
            "fieldType": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Field type"
                },
                "attribute": "field_type",
                "reflect": false
            },
            "min": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "minimum length of input"
                },
                "attribute": "min_value",
                "reflect": false,
                "defaultValue": "-1"
            },
            "max": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "maximum length of input"
                },
                "attribute": "max_value",
                "reflect": false,
                "defaultValue": "-1"
            },
            "referenceType": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Reference type"
                },
                "attribute": "reference-type",
                "reflect": false
            },
            "helpText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Help text"
                },
                "attribute": "help-text",
                "reflect": false
            },
            "googleApiToken": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Google API token"
                },
                "attribute": "google-api-token",
                "reflect": false
            },
            "isAutocompleteEnabled": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is autocomplete enabled"
                },
                "attribute": "is-autocomplete-enabled",
                "reflect": false
            },
            "answeredBySource": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | undefined",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Specifically how the question was answered (ie. \"ACORD 126\")"
                },
                "attribute": "answered-by-source",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "autoComplete": {},
            "useExternal": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "referenceType",
                "methodName": "onReferenceTypeChange"
            }, {
                "propName": "code",
                "methodName": "onCodeChange"
            }, {
                "propName": "fieldType",
                "methodName": "onFieldTypeChange"
            }, {
                "propName": "isAutocompleteEnabled",
                "methodName": "onIsAutocompleteEnabledChange"
            }];
    }
    static get listeners() {
        return [{
                "name": "placesUpdate",
                "method": "onPlacesUpdate",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
