import React__default from 'react';
import { u as useDataListContext } from './DataListContext-es.js';
import { C as Combobox } from './Combobox-es.js';
import './ComboboxProvider-es.js';
import { C as Chip } from './Chip-es.js';
import './tslib.es6-es.js';
import { I as Icon } from './Icon-es.js';

function DataListSort() {
    const { sorting, headers } = useDataListContext();
    if (!sorting)
        return null;
    const { sortable, state, onSort } = sorting;
    const sortByOptions = getSortByOptions();
    return (React__default.createElement(Combobox, { onSelect: selection => handleKeyChange(selection[0].id.toString()), selected: [
            {
                id: getSelectedSortID(),
                label: (state === null || state === void 0 ? void 0 : state.order) || "",
            },
        ] },
        React__default.createElement(Combobox.Activator, null,
            React__default.createElement(Chip, { heading: "Sort", label: getButtonLabel(), variation: state ? "base" : "subtle" }, !state && (React__default.createElement(Chip.Suffix, null,
                React__default.createElement(Icon, { name: "arrowDown", size: "small" }))))),
        sortByOptions.map(({ label, value }) => (React__default.createElement(Combobox.Option, { key: value, id: value, label: label })))));
    function getSortByOptions() {
        const options = sortable.reduce((acc, sort) => {
            const label = headers[sort.key];
            if (!label)
                return acc;
            const customOptions = sort.options;
            if (customOptions) {
                customOptions.forEach(option => {
                    acc.push({
                        label: option.label || "",
                        value: JSON.stringify({
                            key: sort.key,
                            order: option.order,
                            label: option.label,
                            id: option.id,
                        }),
                    });
                });
                return acc;
            }
            return acc;
        }, []);
        // Inject a none option as the first option
        options.unshift({ label: "None", value: "none" });
        return options;
    }
    function getButtonLabel() {
        const selectedOption = sortByOptions.find(option => option.value === getSelectedSortID());
        return (selectedOption === null || selectedOption === void 0 ? void 0 : selectedOption.label) || "";
    }
    function handleKeyChange(value) {
        if (value && value !== "none") {
            const { key, order, label, id } = JSON.parse(value);
            onSort({ key, order: order, label, id });
            return;
        }
        onSort(undefined);
    }
    function getSelectedSortID() {
        const selectedSortID = {
            key: state === null || state === void 0 ? void 0 : state.key,
            order: state === null || state === void 0 ? void 0 : state.order,
            label: state === null || state === void 0 ? void 0 : state.label,
            id: state === null || state === void 0 ? void 0 : state.id,
        };
        return JSON.stringify(selectedSortID);
    }
}

export { DataListSort as D };
