import { areDetailedRelevanceChipsEnabled, doesUserOwnApplicationForm, userHasPermission } from "@boldpenguin/sdk-redux";
import { BpSdkPermissionAction, BpSdkPermissions, } from "@boldpenguin/sdk-types";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkBusinessClassificationSelectorListItemContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                showDetailedRelevanceChip: areDetailedRelevanceChipsEnabled(state),
                enableUserApplicationGuard: !!state.config.options.enableUserApplicationGuard,
                locked: state.application.form.locked,
                isOwner: doesUserOwnApplicationForm(state),
                hasPermissionToEdit: userHasPermission(state, {
                    permission: BpSdkPermissions.ApplicationForms,
                    action: BpSdkPermissionAction.Update,
                }),
            };
        };
        this.alias = undefined;
        this.value = undefined;
        this.isSelected = false;
        this.showExplanation = true;
        this.confidence = undefined;
        this.showConfidence = undefined;
        this.choiceId = undefined;
        this.highlights = undefined;
        this.showDetailedRelevanceChip = false;
        this.locked = undefined;
        this.isOwner = undefined;
        this.hasPermissionToEdit = undefined;
        this.enableUserApplicationGuard = undefined;
        this.isBestMatch = undefined;
        this.isoCodes = undefined;
        this.sicCodes = undefined;
        this.useExternal = false;
        this.isLocked = false;
    }
    onEnableUserApplicationGuard(enableUserApplicationGuard) {
        this.updateIsLocked(this.locked, this.isOwner, this.hasPermissionToEdit, enableUserApplicationGuard);
    }
    onHasPermissionToEdit(hasPermission) {
        this.updateIsLocked(this.locked, this.isOwner, hasPermission, this.enableUserApplicationGuard);
    }
    onIsOwner(isOwner) {
        this.updateIsLocked(this.locked, isOwner, this.hasPermissionToEdit, this.enableUserApplicationGuard);
    }
    onLocked(locked) {
        this.updateIsLocked(locked, this.isOwner, this.hasPermissionToEdit, this.enableUserApplicationGuard);
    }
    updateIsLocked(locked, isOwner, hasPermission, enableUserApplicationGuard) {
        this.isLocked = locked || (enableUserApplicationGuard && (!isOwner || !hasPermission));
    }
    resetState() {
        this.showDetailedRelevanceChip = false;
    }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-business-classification-selector-list-item-container');
        customElements.whenDefined('bp-sdk-external-business-classification-selector-list-item-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
        this.updateIsLocked(this.locked, this.isOwner, this.hasPermissionToEdit, this.enableUserApplicationGuard);
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-business-classification-selector-list-item-container", { alias: this.alias, choiceId: this.choiceId, confidence: this.confidence, isSelected: this.isSelected, showConfidence: this.showConfidence, showExplanation: this.showExplanation, value: this.value, highlights: this.highlights, showDetailedRelevanceChip: this.showDetailedRelevanceChip, isBestMatch: this.isBestMatch, locked: this.locked, isOwner: this.isOwner, hasPermissionToEdit: this.hasPermissionToEdit, enableUserApplicationGuard: this.enableUserApplicationGuard }));
    }
    renderInternal() {
        return (h("bp-sdk-business-classification-selector-list-item", { alias: this.alias, choiceId: this.choiceId, confidence: this.confidence, isSelected: this.isSelected, showConfidence: this.showConfidence, showExplanation: this.showExplanation, value: this.value, highlights: this.highlights, showDetailedRelevanceChip: this.showDetailedRelevanceChip, locked: this.isLocked, isBestMatch: this.isBestMatch, isoCodes: this.isoCodes, sicCodes: this.sicCodes }));
    }
    static get is() { return "bp-sdk-business-classification-selector-list-item-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-business-classification-selector-list-item-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-business-classification-selector-list-item-container.css"]
        };
    }
    static get properties() {
        return {
            "alias": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Alias of choice"
                },
                "attribute": "alias",
                "reflect": false
            },
            "value": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Value of choice"
                },
                "attribute": "value",
                "reflect": false
            },
            "isSelected": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is choice selected"
                },
                "attribute": "is-selected",
                "reflect": false,
                "defaultValue": "false"
            },
            "showExplanation": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Should show explanation"
                },
                "attribute": "show-explanation",
                "reflect": false,
                "defaultValue": "true"
            },
            "confidence": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Confidence level"
                },
                "attribute": "confidence",
                "reflect": false
            },
            "showConfidence": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Should show confidence"
                },
                "attribute": "show-confidence",
                "reflect": false
            },
            "choiceId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Choice's id"
                },
                "attribute": "choice_id",
                "reflect": false
            },
            "highlights": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IChoiceHighlight[] | undefined",
                    "resolved": "IChoiceHighlight[]",
                    "references": {
                        "IChoiceHighlight": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IChoiceHighlight"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "List of highlight's"
                }
            },
            "showDetailedRelevanceChip": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Decision to show detailed relevance chip injected by redux"
                },
                "attribute": "show-detailed-relevance-chip",
                "reflect": false,
                "defaultValue": "false"
            },
            "locked": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Application form state injected from redux"
                },
                "attribute": "locked",
                "reflect": false
            },
            "isOwner": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is owner of application"
                },
                "attribute": "is-owner",
                "reflect": false
            },
            "hasPermissionToEdit": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Has permission to edit application"
                },
                "attribute": "has-permission-to-edit",
                "reflect": false
            },
            "enableUserApplicationGuard": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Enabled user check for application"
                },
                "attribute": "enable-user-application-guard",
                "reflect": false
            },
            "isBestMatch": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is best matching choice"
                },
                "attribute": "is-best-match",
                "reflect": false
            },
            "isoCodes": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IChoiceObject[]",
                    "resolved": "IChoiceObject[]",
                    "references": {
                        "IChoiceObject": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IChoiceObject"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "List of matching ISO codes"
                }
            },
            "sicCodes": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IChoiceObject[]",
                    "resolved": "IChoiceObject[]",
                    "references": {
                        "IChoiceObject": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IChoiceObject"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "List of matching SIC codes"
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {},
            "isLocked": {}
        };
    }
    static get watchers() {
        return [{
                "propName": "enableUserApplicationGuard",
                "methodName": "onEnableUserApplicationGuard"
            }, {
                "propName": "hasPermissionToEdit",
                "methodName": "onHasPermissionToEdit"
            }, {
                "propName": "isOwner",
                "methodName": "onIsOwner"
            }, {
                "propName": "locked",
                "methodName": "onLocked"
            }];
    }
}
