import { h, Host } from "@stencil/core";
/**
 * @swedishName Kategorifilter
 */
export class FormCategoryFilter {
    constructor() {
        this.afCategories = [];
        this.afAllCategories = true;
        this.afAllCategoriesSelected = true;
        this.afAllCategoriesText = 'Alla kategorier';
        this.afVisibleCollapsed = 0;
        this.afStartCollapsed = false;
        this.afMultiselect = true;
        this.afHideToggle = false;
        this.selectedCategories = [];
        this.collapsed = false;
    }
    afCategoriesChangeHandler() {
        this.selectedCategories = this.afCategories
            .filter((category) => category.selected)
            .map((category) => category.name);
        if (this.selectedCategories.length === 0 &&
            this.afAllCategories &&
            this.afAllCategoriesSelected)
            this.selectedCategories = [this.afAllCategoriesText];
    }
    componentDidLoad() {
        this.afCategoriesChangeHandler();
        this.collapsed = this.afStartCollapsed;
    }
    displayCategories() {
        if (this.afHideToggle) {
            return [{ name: this.afAllCategoriesText }, ...this.afCategories];
        }
        if (this.afAllCategories) {
            if (this.collapsed &&
                this.afCategories.length + 1 > this.afVisibleCollapsed)
                return [
                    { name: this.afAllCategoriesText },
                    ...this.afCategories.slice(0, this.afVisibleCollapsed - 1)
                ];
            return [{ name: this.afAllCategoriesText }, ...this.afCategories];
        }
        if (this.collapsed && this.afCategories.length > this.afVisibleCollapsed)
            return this.afCategories.slice(0, this.afVisibleCollapsed);
        return this.afCategories;
    }
    toggleCategory(category) {
        if (!this.afMultiselect) {
            if (this.afAllCategories && category.name === this.afAllCategoriesText) {
                if (this.selectedCategories.includes(this.afAllCategoriesText)) {
                    this.selectedCategories = [];
                    this.afOnSelectedCategoryChange.emit([]);
                }
                else {
                    this.selectedCategories = [this.afAllCategoriesText];
                    this.afOnSelectedCategoryChange.emit(this.afCategories.map((category) => category.name));
                }
            }
            // Not allCategories
            this.selectedCategories = [category.name];
            this.afOnSelectedCategoryChange.emit(this.selectedCategories);
            return;
        }
        if (this.afAllCategories && category.name === this.afAllCategoriesText) {
            if (this.selectedCategories.includes(this.afAllCategoriesText)) {
                this.selectedCategories = [];
                this.afOnSelectedCategoryChange.emit([]);
            }
            else {
                this.selectedCategories = [this.afAllCategoriesText];
                this.afOnSelectedCategoryChange.emit(this.afCategories.map((category) => category.name));
            }
        }
        else {
            if (this.selectedCategories.findIndex((name) => name === category.name) >= 0)
                this.selectedCategories = this.selectedCategories.filter((name) => name !== category.name);
            else if (this.afAllCategories)
                this.selectedCategories = [
                    ...this.selectedCategories.filter((name) => name !== this.afAllCategoriesText),
                    category.name
                ];
            else
                this.selectedCategories = [...this.selectedCategories, category.name];
            this.afOnSelectedCategoryChange.emit(this.selectedCategories);
        }
    }
    toggleCollapsed() {
        this.collapsed = !this.collapsed;
    }
    toggleHidden() {
        this.afHideToggle = !this.toggleHidden;
    }
    render() {
        return (h(Host, { key: 'a9bf071c9972f6ae732f8dfa7b5f3481548dd77b' }, h("div", { key: 'c7fa2dbbe1305e65989c31e7450ead6e4122f8df', class: {
                'digi-form-category-filter': true
            } }, this.displayCategories().map((category) => (h("digi-button", { class: {
                'digi-form-category-filter--category': true,
                'digi-form-category-filter--category--not-selected': !this.selectedCategories.includes(category.name)
            }, afVariation: "primary", afAriaPressed: this.selectedCategories.includes(category.name), onAfOnClick: () => {
                this.toggleCategory(category);
            } }, typeof category.hits === 'undefined' && category.name, typeof category.hits !== 'undefined' && (h("span", null, category.name, ' ', h("span", { class: "digi-form-category-filter--category--thin-text" }, "(", category.hits, ")")))))), this.afHideToggle === false &&
            this.afCategories.length > this.afVisibleCollapsed && (h("div", { key: '2549c0fe3b8c9364be6614bdc965f039bb0aadcb' }, h("digi-button", { key: '3a4bb85891ff201ab27495dba557e0e5de884cbf', afVariation: "function", afAriaExpanded: !this.collapsed, onClick: () => {
                this.toggleCollapsed();
            } }, this.collapsed ? 'Visa alla' : 'Visa färre', this.collapsed ? (h("digi-icon-plus", { slot: "icon", "aria-hidden": "true" })) : (h("digi-icon-minus", { slot: "icon", "aria-hidden": "true" }))))))));
    }
    static get is() { return "digi-form-category-filter"; }
    static get encapsulation() { return "scoped"; }
    static get originalStyleUrls() {
        return {
            "$": ["form-category-filter.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["form-category-filter.css"]
        };
    }
    static get properties() {
        return {
            "afCategories": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "Array<FormCategory>",
                    "resolved": "FormCategory[]",
                    "references": {
                        "Array": {
                            "location": "global",
                            "id": "global::Array"
                        },
                        "FormCategory": {
                            "location": "import",
                            "path": "./form-category-filter.interfaces",
                            "id": "src/components/_form/form-category-filter/form-category-filter.interfaces.ts::FormCategory"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "A list of categories"
                        }],
                    "text": "En lista av kategorier"
                },
                "getter": false,
                "setter": false,
                "defaultValue": "[]"
            },
            "afAllCategories": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "If the list should start with a 'All categories' category"
                        }],
                    "text": "Ifall listan ska b\u00F6rja med en 'Alla kategorier' kategori"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-all-categories",
                "reflect": false,
                "defaultValue": "true"
            },
            "afAllCategoriesSelected": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "If the 'All categories' category should start selected"
                        }],
                    "text": "Ifall 'Alla kategorier' kategorin ska b\u00F6rja vald"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-all-categories-selected",
                "reflect": false,
                "defaultValue": "true"
            },
            "afAllCategoriesText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "The name of the 'All categories' category"
                        }],
                    "text": "Namnet p\u00E5 'Alla kategorier' kategorin"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-all-categories-text",
                "reflect": false,
                "defaultValue": "'Alla kategorier'"
            },
            "afVisibleCollapsed": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Number of visible categories when the list is collapsed (the 'Show more/less'-button is not shown if there are fewer selectable categories than this number)"
                        }],
                    "text": "Antal synliga kategorier n\u00E4r listan \u00E4r kollapsad (ifall det finns f\u00E4rre valbara kategorier \u00E4n detta v\u00E4rde s\u00E5 syns inte 'Visa fler/f\u00E4rre'-knappen)"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-visible-collapsed",
                "reflect": false,
                "defaultValue": "0"
            },
            "afStartCollapsed": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "If the list should start out collapsed"
                        }],
                    "text": "Ifall listan ska var kollapsad fr\u00E5n b\u00F6rjan"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-start-collapsed",
                "reflect": false,
                "defaultValue": "false"
            },
            "afMultiselect": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Set if filtering should use multiselect."
                        }],
                    "text": "V\u00E4lj om man ska kunna filtrera p\u00E5 flera kategorier \u00E5t g\u00E5ngen."
                },
                "getter": false,
                "setter": false,
                "attribute": "af-multiselect",
                "reflect": false,
                "defaultValue": "true"
            },
            "afHideToggle": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Show all categories and hide button to \"visa fler\"/\"visa f\u00E4rre\""
                        }],
                    "text": "Visa alla kategorier och d\u00F6lj knappen f\u00F6r att \"visa fler/visa f\u00E4rre"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-hide-toggle",
                "reflect": false,
                "defaultValue": "false"
            }
        };
    }
    static get states() {
        return {
            "selectedCategories": {},
            "collapsed": {}
        };
    }
    static get events() {
        return [{
                "method": "afOnSelectedCategoryChange",
                "name": "afOnSelectedCategoryChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "When selected categories is changed"
                        }],
                    "text": "Sker vid \u00E4ndrade valda kategorier"
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "hostElement"; }
    static get watchers() {
        return [{
                "propName": "afCategories",
                "methodName": "afCategoriesChangeHandler"
            }];
    }
}
