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 Insurances = function (_a) {
    var filters = _a.filters, t = _a.t, onFilterApply = _a.onFilterApply, closeModal = _a.closeModal;
    var fpinsurance = filters.fpinsurance, isOrthoDoc = filters.isOrthoDoc;
    var insurancesFilterOptions = useAppSelector(function (state) { return state.config.config; }).insurancesFilterOptions;
    var _b = __read(useState(fpinsurance && (fpinsurance === null || fpinsurance === void 0 ? void 0 : fpinsurance.length) > 5 ? true : false), 2), showMoreInsurances = _b[0], setShowMoreInsurances = _b[1];
    var _c = __read(useState(fpinsurance || []), 2), selectedInsurancesForFilter = _c[0], setSelectedInsurancesForFilter = _c[1];
    var applyFilter = function () {
        onFilterApply('isOrthoDoc', isOrthoDoc, [
            { key: 'fpinsurance', value: selectedInsurancesForFilter },
        ]);
        closeModal();
    };
    var clearFilters = function () {
        setSelectedInsurancesForFilter([]);
        onFilterApply('isOrthoDoc', isOrthoDoc, [{ key: 'fpinsurance', value: [] }]);
        closeModal();
    };
    return (_jsxs("div", { className: cx('form-group filter-specialty'), children: [_jsx("label", { className: "filter-title", children: t.result_sort_insurance }), _jsxs("div", { className: cx('checkbox-wrap'), children: [insurancesFilterOptions.slice(0, !showMoreInsurances ? 5 : undefined).map(function (item) {
                        return (_jsx(CheckBox, { checked: selectedInsurancesForFilter.includes(item.value), onChange: function (v) {
                                if (!v) {
                                    var filtered = selectedInsurancesForFilter.filter(function (lang) {
                                        return lang !== item.value;
                                    });
                                    setSelectedInsurancesForFilter(filtered);
                                }
                                else {
                                    setSelectedInsurancesForFilter(function (prev) { return __spreadArray(__spreadArray([], __read(prev), false), [item.value], false); });
                                }
                            }, label: item.translation, name: "vivera", width: "100%" }));
                    }), insurancesFilterOptions.length > 5 && (_jsx("span", { className: cx('more_options'), onClick: function () { return setShowMoreInsurances(!showMoreInsurances); }, children: showMoreInsurances ? 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 Insurances;
