var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import CheckBox from 'src/components/CheckBox2/CheckBox';
import Button from 'src/components/Button';
import { useAppSelector } from 'src/app/redux/store';
import css from '../FilterBar.module.scss';
import { _cx } from 'src/utility/class';
var cx = _cx(css);
var Languages = function (_a) {
    var filters = _a.filters, t = _a.t, onFilterApply = _a.onFilterApply, closeModal = _a.closeModal;
    var flan = filters.flan, isOrthoDoc = filters.isOrthoDoc;
    var lanuagesFilterOptions = useAppSelector(function (state) { return state.config.config; }).lanuagesFilterOptions;
    var _b = __read(useState(flan && (flan === null || flan === void 0 ? void 0 : flan.length) > 5 ? true : false), 2), showMoreLangs = _b[0], setShowMoreLangs = _b[1];
    var _c = __read(useState(flan || []), 2), selectedLangsForFilter = _c[0], setSelectedLangsForFilter = _c[1];
    var applyFilter = function () {
        onFilterApply('isOrthoDoc', isOrthoDoc, [{ key: 'flan', value: selectedLangsForFilter }]);
        closeModal();
    };
    var clearFilters = function () {
        setSelectedLangsForFilter([]);
        onFilterApply('isOrthoDoc', isOrthoDoc, [{ key: 'flan', value: [] }]);
        closeModal();
    };
    return (_jsxs("div", { className: cx('form-group filter-specialty'), children: [_jsx("label", { className: "filter-title", children: t.result_sort_languages }), _jsxs("div", { className: cx('checkbox-wrap'), children: [lanuagesFilterOptions.slice(0, !showMoreLangs ? 5 : undefined).map(function (item) {
                        return (_jsx(CheckBox, { checked: selectedLangsForFilter.includes(item.value), onChange: function (v) {
                                if (!v) {
                                    var filtered = selectedLangsForFilter.filter(function (lang) {
                                        return lang !== item.value;
                                    });
                                    setSelectedLangsForFilter(filtered);
                                }
                                else {
                                    setSelectedLangsForFilter(function (prev) { return __spreadArray(__spreadArray([], __read(prev), false), [item.value], false); });
                                }
                            }, label: item.translation, name: "vivera", width: "100%" }));
                    }), lanuagesFilterOptions.length > 5 && (_jsx("span", { className: cx('more_options'), onClick: function () { return setShowMoreLangs(!showMoreLangs); }, children: showMoreLangs ? t.result_less_options : t.result_more_options }))] }), _jsx("hr", {}), _jsxs("div", { className: "flex-space-between", children: [_jsx(Button, { type: "submit", styleType: "cancel", onClick: clearFilters, children: t.result_clear_filter }), _jsx(Button, { type: "submit", styleType: "normal", onClick: applyFilter, children: t.result_filter_button_apply })] })] }));
};
export default Languages;
