import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/*
* Copyright (c) 2020-present SMC Treviso s.r.l. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
import { faChevronDown, faChevronUp } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { isEqualWith } from "lodash";
import React from "react";
import { remappingLanguage } from "../embeddable/Main";
import { ChangeLanguage } from "./ChangeLanguage";
import { FiltersMemo } from "./Filters";
import { SortResultListMemo } from "./SortResultList";
import { useTranslation } from "react-i18next";
import { DataRangePickerVertical } from "./DateRangePickerVertical";
import { css } from "styled-components";
function CollapsableFilterCategoryLocal({ title, description, children, isOpenFilter = false, }) {
    const [isOpen, setIsOpen] = React.useState(isOpenFilter);
    return (_jsxs("div", { className: "openk9-filter-category", css: css `
        padding-inline: 16px;
        padding-bottom: 16px;
      `, children: [_jsxs("div", { className: "openk9-filter-category-title", css: css `
          user-select: none;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 8px;
          padding: 6px 0;
          padding-bottom: 8px;
          border-bottom: 1px solid var(--openk9-embeddable-search--border-color);
        `, children: [_jsx("legend", { className: "legend-filters", css: css `
            display: flex;
            align-items: center;
            gap: 8px;
            :first-letter {
              text-transform: uppercase;
            }
          `, children: _jsx("strong", { className: "name-category-filter", css: css `
              font-size: 14px;
              letter-spacing: 0.2px;
              color: var(--openk9-embeddable-search--secondary-text-color);
            `, children: title }) }), _jsx("button", { className: `openk9-collapsable-filters ${isOpen
                            ? "openk9-dropdown-filters-open"
                            : "openk9-dropdown-filters-close"}`, "aria-label": "Collassa filtro", "aria-expanded": isOpen ? "true" : "false", css: css `
            background: transparent;
            border: 1px solid var(--openk9-embeddable-search--border-color);
            border-radius: 8px;
            padding: 6px 8px;
            cursor: pointer;
            transition: transform 120ms ease, background-color 120ms ease,
              border-color 120ms ease;
            &:hover {
              background: rgba(0, 0, 0, 0.03);
            }
            &:active {
              transform: translateY(1px);
            }
          `, onClick: () => setIsOpen((prev) => !prev), children: _jsx(FontAwesomeIcon, { icon: isOpen ? faChevronUp : faChevronDown, css: css `
              color: var(--openk9-embeddable-search--secondary-icon-color);
              cursor: pointer;
            ` }) })] }), description && (_jsx("div", { className: "openk9-filter-category-description", css: css `
            font-size: 12px;
            color: var(--openk9-embeddable-search--secondary-text-color);
            margin-bottom: 8px;
          `, children: description })), isOpen && (_jsx("div", { className: "openk9-filter-category-content", css: css `
            padding-top: 16px;
          `, children: children }))] }));
}
function FactoryFilter(filter, filtersSelect, onSelect, lang, sort, filterDefault, calendar) {
    switch (filter) {
        case "calendar":
            return (_jsx(CollapsableFilterCategoryLocal, { title: calendar.translationLabel?.calendarLabel || "Calendar", isOpenFilter: true, children: _jsx(DataRangePickerVertical, { language: lang.lang || "it", calendarDate: calendar.calendarDate, onChange: (e) => {
                        calendar.setCalendarSelected(e);
                    }, translationLabel: calendar.translationLabel }) }));
        case "language":
            return (_jsx(CollapsableFilterCategoryLocal, { title: "Language", children: _jsx(ChangeLanguage, { activeLanguage: lang.lang || "it", setChangeLanguage: (language) => {
                        lang.setLang(language);
                    }, languages: lang.languages }) }));
        case "sort":
            return (_jsx(CollapsableFilterCategoryLocal, { title: "Sort", children: _jsx(SortResultListMemo, { language: lang.lang || undefined, setSortResult: sort.setSort }) }));
        case "filters":
            return (_jsxs(_Fragment, { children: [_jsx("span", { className: "openk9-Filters-informations", children: calendar?.translationLabel?.filtersLabel || "Filters" }), _jsx(FiltersMemo, { ...filterDefault, numberItems: filterDefault.numberResultOfFilters, searchQuery: filtersSelect || [], onAddFilterToken: (token) => onSelect((t) => [...(t ?? []), token]), onRemoveFilterToken: (token) => onSelect((t) => t?.filter((i) => !isEqualWith(i.values, token.values))), dynamicFilters: false, isOpenFilter: true })] }));
        default:
            return null;
    }
}
export default function AllFilters({ filtersUse, defaultLanguage, filterDefault, calendar, callback, }) {
    const [searchToken, setSearchToken] = React.useState(filterDefault.defaultFilter || []);
    const [lang, setLang] = React.useState(defaultLanguage);
    const [calendarToken, setCalendarToken] = React.useState({
        endDate: undefined,
        startDate: undefined,
        keywordKey: undefined,
    });
    const { i18n } = useTranslation();
    const [sort, setSort] = React.useState(null);
    const orderedFilters = React.useMemo(() => (filtersUse ? [...filtersUse].sort((a, b) => a.sort - b.sort) : []), [filtersUse]);
    return (_jsxs(_Fragment, { children: [orderedFilters?.map((filter) => FactoryFilter(filter.filter, searchToken, setSearchToken, { lang, setLang, languages: filterDefault.languages }, { sort, setSort }, filterDefault, {
                calendarDate: calendarToken,
                setCalendarSelected: setCalendarToken,
                translationLabel: calendar.translationLabel,
            })), _jsxs("div", { className: "container-openk9-all-filters", children: [_jsx("button", { className: "Openk9-all-filters-reset", "aria-label": calendar.translationLabel?.ariaLabelReset || "Reset Filters", onClick: () => {
                            setSearchToken([]);
                            setLang(defaultLanguage);
                            if (callback && callback.reset) {
                                callback.reset();
                            }
                            i18n.changeLanguage(remappingLanguage({ language: defaultLanguage }));
                            setSort(null);
                            setCalendarToken({
                                endDate: undefined,
                                startDate: undefined,
                                keywordKey: undefined,
                            });
                        }, children: calendar.translationLabel?.buttonResetFilters || "Reset Filters" }), _jsx("button", { className: "Openk9-all-filters-save", "aria-label": calendar.translationLabel?.ariaLabelSave || "Save Filters", onClick: () => {
                            filterDefault.setAllFilters(searchToken || []);
                            filterDefault.setLanguageSelected(lang || "it");
                            i18n.changeLanguage(remappingLanguage({ language: lang || "it" }));
                            filterDefault.setSortSelected(sort || null);
                            calendar.onChange(calendarToken);
                            if (callback && callback.save) {
                                callback.save();
                            }
                        }, css: css `
            background: var(--openk9-embeddable-search--primary-color, #0078d4);
            color: #fff;
            border: none;
            border-radius: 8px;
            padding: 10px 24px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
            transition: background 0.2s, box-shadow 0.2s;
            margin-top: 16px;
            &:hover {
              background: var(
                --openk9-embeddable-search--primary-light-color,
                #005fa3
              );
              box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
            }
            &:active {
              background: var(
                --openk9-embeddable-search--primary-color-active,
                #004377
              );
            }
          `, children: calendar.translationLabel?.buttonSaveFilters || "Save" })] })] }));
}
