import { jsx as _jsx } 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 { useState, useEffect } from "react";
import SelectComponent from "./Select";
const mockOptions = [
    {
        field: "name",
        id: 1,
        isDefault: true,
        type: "asc",
        label: "Name",
        translationMap: {
            "label.en_US": "Name",
            "label.it_IT": "Nome",
        },
    },
    {
        field: "price",
        id: 2,
        isDefault: false,
        type: "desc",
        label: "Price",
        translationMap: {
            "label.en_US": "Price",
            "label.it_IT": "Prezzo",
        },
    },
];
const SortResults = ({ selectOptions, extraClass = "", labelDefault = "Select Option", language, labelText, setSort, classNameLabel, sort, useMockData = false, }) => {
    const [options, setOptions] = useState(useMockData ? mockOptions : selectOptions);
    useEffect(() => {
        if (!useMockData) {
            setOptions(selectOptions);
        }
    }, [selectOptions, useMockData]);
    const handleChange = (event) => {
        const value = event.target.value;
        const [field, type] = value.split("-");
        if (field && type) {
            setSort({ field, type: type });
        }
        else {
            setSort(null);
        }
    };
    const sortedOptions = [...options].sort((a, b) => Number(b.isDefault) - Number(a.isDefault));
    return (_jsx(SelectComponent, { handleChange: handleChange, language: language, selectOptions: sortedOptions, extraClass: extraClass, labelDefault: labelDefault, label: labelText, classLabel: classNameLabel, selectedSort: sort?.sort }));
};
export default SortResults;
