"use client";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Section } from "./ui/Section";
import { useEditContext } from "./client/editContext";
import { FieldListFieldWithFallbacks } from "./FieldListFieldWithFallbacks";
import { FilterInput } from "../components/FilterInput";
import { Fragment, useState } from "react";
import uuid from "react-uuid";
import { Checkbox } from "../components/ui/checkbox";
import { ChevronRight } from "lucide-react";
import { Tooltip, TooltipContent, TooltipTrigger, } from "../components/ui/tooltip";
export function FieldList({ fields, compareToFields, sections, validators, filter, simplified, showStandardFieldsEnabled, readonly, }) {
    const editContext = useEditContext();
    const [searchFilter, setSearchFilter] = useState("");
    const [showStandardFields, setShowStandardFields] = useState(false);
    const uniqueId = uuid();
    if (!editContext)
        return;
    if (!fields)
        return;
    return (_jsxs("div", { className: "text-neutral-grey-100", children: [_jsxs("div", { className: "tour-field-list border-border-default sticky top-0 z-10 flex items-center gap-2 border-b bg-white p-2", children: [_jsx(FilterInput, { className: "flex-1", value: searchFilter, onChange: setSearchFilter, placeholder: "Filter fields..." }), showStandardFieldsEnabled && (_jsx(_Fragment, { children: _jsxs("div", { className: "flex items-center gap-1", children: [_jsx(Checkbox, { id: uniqueId, checked: showStandardFields, onCheckedChange: (checked) => setShowStandardFields(checked) }), " ", _jsx("label", { htmlFor: uniqueId, className: "text-neutral-grey-100 cursor-pointer text-xs", children: "Show standard fields" })] }) }))] }), fields.map((itemFields, index) => (_jsxs(Fragment, { children: [itemFields.headline && (_jsxs("div", { className: "mt-2 border-b py-2", children: [_jsxs("div", { className: "flex items-center gap-1 text-sm", children: [_jsx(ChevronRight, { className: "h-4 w-4" }), itemFields.icon && (_jsx("img", { src: itemFields.icon, alt: "", className: "h-4 w-4 object-contain" })), itemFields.headline] }), itemFields.path && (_jsx("div", { className: "text-neutral-grey-50 mt-1 ml-5 text-xs", children: itemFields.fullPath ? (_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx("span", { className: "cursor-help", children: itemFields.path }) }), _jsx(TooltipContent, { children: itemFields.fullPath })] })) : (itemFields.path) }))] })), _jsx(FieldListForSingleItem, { fields: itemFields.fields, validators: validators, filter: filter, searchFilter: searchFilter, showStandardFields: showStandardFields, simplified: simplified, sections: sections, compareToFields: compareToFields, readonly: readonly }, index)] }, index)))] }));
}
function FieldListForSingleItem({ fields, validators, filter, searchFilter, simplified, sections, showStandardFields, compareToFields, readonly, }) {
    let fieldsArray = Array.isArray(fields)
        ? fields
        : Object.values(fields);
    // Create section order mapping before any filtering to preserve original order
    const originalSectionOrder = new Map();
    fieldsArray.forEach((field) => {
        const section = field.section || "";
        const sectionSortOrder = field.sectionSortOrder || 0;
        // Use the minimum sectionSortOrder for each section to ensure consistent ordering
        if (!originalSectionOrder.has(section)) {
            originalSectionOrder.set(section, sectionSortOrder);
        }
        else {
            const existing = originalSectionOrder.get(section);
            originalSectionOrder.set(section, Math.min(existing, sectionSortOrder));
        }
    });
    if (filter)
        fieldsArray = fieldsArray.filter(filter);
    if (searchFilter) {
        const searchFilterLower = searchFilter.toLowerCase();
        fieldsArray = fieldsArray.filter((x) => x.name?.toLowerCase().includes(searchFilterLower) ||
            x.displayName?.toLowerCase().includes(searchFilterLower));
    }
    if (!showStandardFields) {
        fieldsArray = fieldsArray.filter((x) => !x.name?.startsWith("__"));
    }
    const sectionsGrouping = fieldsArray.reduce((acc, value) => {
        // Group initialization
        const section = value.section || "";
        if (!acc[section]) {
            acc[section] = [];
        }
        // Only add fields that have a valid descriptor item id
        if (value.descriptor.item.id) {
            acc[section].push(value);
        }
        return acc;
    }, {});
    const sectionList = Object.values(sectionsGrouping);
    // Sort sections using the original section order, not the first field after filtering
    sectionList.sort((a, b) => {
        const sectionA = a[0]?.section || "";
        const sectionB = b[0]?.section || "";
        const orderA = originalSectionOrder.get(sectionA) || 0;
        const orderB = originalSectionOrder.get(sectionB) || 0;
        return orderA - orderB;
    });
    function getSectionFields(fields, startTabIndex) {
        fields.sort((a, b) => a?.sortOrder - b?.sortOrder);
        return fields.map((field, index) => (_jsx(FieldListFieldWithFallbacks, { fieldDescriptor: field.descriptor, simplified: simplified, validators: validators.filter((x) => x.fieldId === field.id), compareToField: compareToFields?.find((x) => x.id === field.id), readonly: readonly, tabIndex: startTabIndex + index }, field.id)));
    }
    // Calculate tab indices across all sections
    let currentTabIndex = 0;
    return sectionList
        .filter((sectionFields) => {
        const section = sectionFields[0]?.section;
        return !sections || sections.includes(section);
    })
        .map((sectionFields) => {
        const section = sectionFields[0]?.section;
        const startIndex = currentTabIndex;
        currentTabIndex += sectionFields.length;
        return simplified ? (getSectionFields(sectionFields, startIndex)) : (_jsx(Section, { title: section, children: getSectionFields(sectionFields, startIndex) }, section));
    });
}
//# sourceMappingURL=FieldList.js.map