/*!
 * Built by ShiftSoftware
 * Copyright (c)
 */
import { Host, h } from "@stencil/core";
import deadStockSchema from "../../locales/partLookup/deadStock/type";
import { getMockFile } from "../../features/mocks/index";
import { VehicleInfoLayout } from "../../features/vehicle-info-layout/index";
import { smartInvokable } from "../../features/blazor-ref/index";
import { setPartLookupData, setPartLookupErrorState } from "../../features/part-lookup-components/index";
import { getLocaleLanguage, getSharedLocal, sharedLocalesSchema } from "../../features/multi-lingual/index";
import { ArrowUpIcon } from "../../global/assets/arrow-up-icon";
import { EmptyTableIcon } from "../../global/assets/empty-table-icon";
import { DeadStockItem } from "./components/dead-stock-item";
export class DeadStockLookup {
    constructor() {
        // #region Localization
        this.language = 'en';
        this.locale = Object.assign({ sharedLocales: sharedLocalesSchema.getDefault() }, deadStockSchema.getDefault());
        // #endregion
        // #region Vehicle info layout prop
        this.coreOnly = false;
        // #endregion
        // #region Part Lookup Component Shared Logic
        this.mockUrl = '';
        this.isError = false;
        this.isLoading = false;
        // #endregion
        // #region Component Logic
        this.openedAccordions = [];
        this.newItemsHasBeenRendered = false;
        this.toggleAccordion = (header) => {
            if (this.isLoading)
                return;
            if (this.openedAccordions.includes(header)) {
                this.openedAccordions = this.openedAccordions.filter(item => item !== header);
            }
            else {
                this.openedAccordions = [...this.openedAccordions, header];
            }
        };
    }
    async componentWillLoad() {
        await Promise.all([this.changeLanguage(this.language), this.onIsDevChange(this.isDev)]);
    }
    async changeLanguage(newLanguage) {
        const [sharedLocales, locale] = await Promise.all([getSharedLocal(newLanguage), getLocaleLanguage(newLanguage, 'partLookup.deadStock', deadStockSchema)]);
        this.locale = Object.assign({ sharedLocales }, locale);
    }
    async setBlazorRef(newBlazorRef) {
        this.blazorRef = newBlazorRef;
    }
    async setMockData(newMockData) {
        this.mockData = newMockData;
        smartInvokable.bind(this)(this.loadedMockDatas, newMockData);
    }
    async getMockData() {
        return this.mockData;
    }
    async onIsDevChange(isDev) {
        if (!isDev)
            return;
        const mockData = await getMockFile('part-lookup', this.mockUrl);
        await this.setMockData(mockData);
    }
    async fetchData(newData) {
        await setPartLookupData(this, newData, {
            beforeAssignment: async (response) => {
                await new Promise(r => setTimeout(r, 500));
                this.openedAccordions = [];
                await new Promise(r => setTimeout(r, 500));
                return response;
            },
        });
    }
    async setErrorMessage(message) {
        setPartLookupErrorState(this, message);
    }
    onLoadingChange(newValue) {
        smartInvokable.bind(this)(this.loadingStateChange, newValue);
    }
    async loadingListener(newIsLoading) {
        if (newIsLoading)
            this.newItemsHasBeenRendered = false;
        else {
            await new Promise(r => setTimeout(r, 100));
            this.newItemsHasBeenRendered = true;
        }
    }
    // #endregion
    render() {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
        return (h(Host, { key: 'd62c70833921bd1b5765a2d7e072bbc5dbded2ac', translate: "no" }, h(VehicleInfoLayout, { key: '309b40614e429189a8369c1248f6f273269f4955', isError: this.isError, coreOnly: this.coreOnly, isLoading: this.isLoading, header: (_a = this.partLookup) === null || _a === void 0 ? void 0 : _a.partNumber, direction: this.locale.sharedLocales.direction, errorMessage: this.locale.sharedLocales.errors[this.errorMessage] || this.locale.sharedLocales.errors.wildCard }, h("div", { key: '080259257a66be6dfd2b9c750cdd4a71f070dc04', class: "p-[16px]" }, h("div", { key: 'd101e493bf7127a8da1614603fe920e60151d8ad', class: "pb-[16px]" }, h(DeadStockItem, { locale: this.locale, key: "first-dead-stock", toggleAccordion: this.toggleAccordion, isOpened: !this.isLoading && ((_c = (_b = this.partLookup) === null || _b === void 0 ? void 0 : _b.deadStock) === null || _c === void 0 ? void 0 : _c.length) > 0 ? this.openedAccordions.includes((_e = (_d = this.partLookup) === null || _d === void 0 ? void 0 : _d.deadStock[0]) === null || _e === void 0 ? void 0 : _e.companyName) : false, item: ((_g = (_f = this.partLookup) === null || _f === void 0 ? void 0 : _f.deadStock) === null || _g === void 0 ? void 0 : _g.length) > 0
                ? (_h = this.partLookup) === null || _h === void 0 ? void 0 : _h.deadStock[0]
                : { companyName: !!this.partLookup && !((_j = this.partLookup) === null || _j === void 0 ? void 0 : _j.deadStock) ? this.locale.sharedLocales.noData : '' }, icon: (!this.partLookup && !((_k = this.partLookup) === null || _k === void 0 ? void 0 : _k.deadStock) && h("div", null)) ||
                (!!this.partLookup && !((_l = this.partLookup) === null || _l === void 0 ? void 0 : _l.deadStock) && h(EmptyTableIcon, null)) ||
                (!!this.partLookup && !!((_m = this.partLookup) === null || _m === void 0 ? void 0 : _m.deadStock) && h(ArrowUpIcon, null)) })), h("flexible-container", { key: 'ee3802a22b13bfa0ebec4bdd67e8f9c4f2c3f706', isOpened: this.newItemsHasBeenRendered }, h("div", { key: 'c68340e2c11be77e5cde04ece671c8a6df8b8289', class: "flex flex-col gap-[16px]" }, ((_p = (_o = this.partLookup) === null || _o === void 0 ? void 0 : _o.deadStock) === null || _p === void 0 ? void 0 : _p.length) > 1 &&
            ((_q = this.partLookup) === null || _q === void 0 ? void 0 : _q.deadStock.slice(1).map(deadStock => (h(DeadStockItem, { item: deadStock, locale: this.locale, icon: h(ArrowUpIcon, null), toggleAccordion: this.toggleAccordion, isOpened: this.openedAccordions.includes(deadStock === null || deadStock === void 0 ? void 0 : deadStock.companyName) }))))))))));
    }
    static get is() { return "dead-stock-lookup"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["dead-stock-lookup.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["dead-stock-lookup.css"]
        };
    }
    static get properties() {
        return {
            "language": {
                "type": "string",
                "attribute": "language",
                "mutable": false,
                "complexType": {
                    "original": "LanguageKeys",
                    "resolved": "\"ar\" | \"en\" | \"ku\" | \"ru\"",
                    "references": {
                        "LanguageKeys": {
                            "location": "import",
                            "path": "~features/multi-lingual",
                            "id": "src/features/multi-lingual/index.ts::LanguageKeys"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "'en'"
            },
            "coreOnly": {
                "type": "boolean",
                "attribute": "core-only",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "false"
            },
            "mockUrl": {
                "type": "string",
                "attribute": "mock-url",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "''"
            },
            "isDev": {
                "type": "boolean",
                "attribute": "is-dev",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "endpoint": {
                "type": "string",
                "attribute": "endpoint",
                "mutable": false,
                "complexType": {
                    "original": "Endpoint",
                    "resolved": "string | { url: string; body?: object; headers?: object; method?: string; query?: QueryString; }",
                    "references": {
                        "Endpoint": {
                            "location": "import",
                            "path": "~lib/fetch-from",
                            "id": "src/global/lib/fetch-from.ts::Endpoint"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "loadedMockDatas": {
                "type": "string",
                "attribute": "loaded-mock-datas",
                "mutable": true,
                "complexType": {
                    "original": "BlazorInvokableFunction<(response: any) => void>",
                    "resolved": "((response: any) => void) | string",
                    "references": {
                        "BlazorInvokableFunction": {
                            "location": "import",
                            "path": "~features/blazor-ref",
                            "id": "src/features/blazor-ref/index.ts::BlazorInvokableFunction"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "errorCallback": {
                "type": "string",
                "attribute": "error-callback",
                "mutable": true,
                "complexType": {
                    "original": "BlazorInvokableFunction<(errorMessage: ErrorKeys) => void>",
                    "resolved": "((errorMessage: OptionalKeys<{ noBaseUrl: string; invalidVin: string; vinNumberRequired: string; partNumberRequired: string; wrongResponseFormat: string; noPartsFound: string; noServiceAvailable: string; wrongFormStructure: string; wildCard: string; requestFailedPleaseTryAgainLater: string; }>) => void) | string",
                    "references": {
                        "BlazorInvokableFunction": {
                            "location": "import",
                            "path": "~features/blazor-ref",
                            "id": "src/features/blazor-ref/index.ts::BlazorInvokableFunction"
                        },
                        "ErrorKeys": {
                            "location": "import",
                            "path": "~features/multi-lingual",
                            "id": "src/features/multi-lingual/index.ts::ErrorKeys"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "loadingStateChange": {
                "type": "string",
                "attribute": "loading-state-change",
                "mutable": true,
                "complexType": {
                    "original": "BlazorInvokableFunction<(isLoading: boolean) => void>",
                    "resolved": "((isLoading: boolean) => void) | string",
                    "references": {
                        "BlazorInvokableFunction": {
                            "location": "import",
                            "path": "~features/blazor-ref",
                            "id": "src/features/blazor-ref/index.ts::BlazorInvokableFunction"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "loadedResponse": {
                "type": "string",
                "attribute": "loaded-response",
                "mutable": true,
                "complexType": {
                    "original": "BlazorInvokableFunction<(response: PartLookupDTO) => void>",
                    "resolved": "((response: PartLookupDTO) => void) | string",
                    "references": {
                        "BlazorInvokableFunction": {
                            "location": "import",
                            "path": "~features/blazor-ref",
                            "id": "src/features/blazor-ref/index.ts::BlazorInvokableFunction"
                        },
                        "PartLookupDTO": {
                            "location": "import",
                            "path": "~types/generated/part/part-lookup-dto",
                            "id": "src/global/types/generated/part/part-lookup-dto.ts::PartLookupDTO"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "locale": {},
            "blazorRef": {},
            "searchString": {},
            "isError": {},
            "errorMessage": {},
            "isLoading": {},
            "partLookup": {},
            "openedAccordions": {},
            "newItemsHasBeenRendered": {}
        };
    }
    static get methods() {
        return {
            "setBlazorRef": {
                "complexType": {
                    "signature": "(newBlazorRef: DotNetObjectReference) => Promise<void>",
                    "parameters": [{
                            "name": "newBlazorRef",
                            "type": "{ invokeMethodAsync: (methodName: string, ...args: any[]) => Promise<any>; }",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "DotNetObjectReference": {
                            "location": "import",
                            "path": "~features/blazor-ref",
                            "id": "src/features/blazor-ref/index.ts::DotNetObjectReference"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            },
            "setMockData": {
                "complexType": {
                    "signature": "(newMockData: PartLookupMock) => Promise<void>",
                    "parameters": [{
                            "name": "newMockData",
                            "type": "{ [key: string]: PartLookupDTO; }",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "PartLookupMock": {
                            "location": "import",
                            "path": "~features/part-lookup-components",
                            "id": "src/features/part-lookup-components/index.ts::PartLookupMock"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            },
            "getMockData": {
                "complexType": {
                    "signature": "() => Promise<any>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<any>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            },
            "fetchData": {
                "complexType": {
                    "signature": "(newData: PartLookupDTO | string) => Promise<void>",
                    "parameters": [{
                            "name": "newData",
                            "type": "string | PartLookupDTO",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "PartLookupDTO": {
                            "location": "import",
                            "path": "~types/generated/part/part-lookup-dto",
                            "id": "src/global/types/generated/part/part-lookup-dto.ts::PartLookupDTO"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            },
            "setErrorMessage": {
                "complexType": {
                    "signature": "(message: ErrorKeys) => Promise<void>",
                    "parameters": [{
                            "name": "message",
                            "type": "\"noBaseUrl\" | \"invalidVin\" | \"vinNumberRequired\" | \"partNumberRequired\" | \"wrongResponseFormat\" | \"noPartsFound\" | \"noServiceAvailable\" | \"wrongFormStructure\" | \"wildCard\" | \"requestFailedPleaseTryAgainLater\"",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "ErrorKeys": {
                            "location": "import",
                            "path": "~features/multi-lingual",
                            "id": "src/features/multi-lingual/index.ts::ErrorKeys"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "language",
                "methodName": "changeLanguage"
            }, {
                "propName": "isDev",
                "methodName": "onIsDevChange"
            }, {
                "propName": "isLoading",
                "methodName": "onLoadingChange"
            }, {
                "propName": "isLoading",
                "methodName": "loadingListener"
            }];
    }
}
