'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import React, { useMemo } from 'react';
import { FieldLabel } from '../../../../fields/FieldLabel/index.js';
import { useField } from '../../../../forms/useField/index.js';
import { useConfig } from '../../../../providers/Config/index.js';
import { GroupByBuilder } from '../../../GroupByBuilder/index.js';
import './index.scss';
export const QueryPresetsGroupByField = ({ field: { label, required } })=>{
    const { path, setValue, value } = useField();
    const relatedCollectionField = useField({
        path: 'relatedCollection'
    });
    const relatedCollection = relatedCollectionField.value;
    const { config } = useConfig();
    const collectionConfig = useMemo(()=>{
        if (!relatedCollection) {
            return null;
        }
        return config.collections?.find((col)=>col.slug === relatedCollection);
    }, [
        relatedCollection,
        config.collections
    ]);
    const fields = collectionConfig?.fields ?? [];
    if (!relatedCollection) {
        return /*#__PURE__*/ _jsxDEV("div", {
            className: "field-type query-preset-group-by-field",
            children: [
                /*#__PURE__*/ _jsxDEV(FieldLabel, {
                    as: "h3",
                    label: label,
                    path: path,
                    required: required
                }, void 0, false, {
                    fileName: "src/elements/QueryPresets/fields/GroupByField/index.tsx",
                    lineNumber: 32,
                    columnNumber: 9
                }, this),
                /*#__PURE__*/ _jsxDEV("p", {
                    className: "query-preset-group-by-field__hint",
                    children: "Select the related collection to configure group by."
                }, void 0, false, {
                    fileName: "src/elements/QueryPresets/fields/GroupByField/index.tsx",
                    lineNumber: 33,
                    columnNumber: 9
                }, this)
            ]
        }, void 0, true, {
            fileName: "src/elements/QueryPresets/fields/GroupByField/index.tsx",
            lineNumber: 31,
            columnNumber: 7
        }, this);
    }
    return /*#__PURE__*/ _jsxDEV("div", {
        className: "field-type query-preset-group-by-field",
        children: [
            /*#__PURE__*/ _jsxDEV(FieldLabel, {
                as: "h3",
                label: label,
                path: path,
                required: required
            }, void 0, false, {
                fileName: "src/elements/QueryPresets/fields/GroupByField/index.tsx",
                lineNumber: 42,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV(GroupByBuilder, {
                collectionSlug: relatedCollection,
                fields: fields,
                onChange: (groupBy)=>setValue(groupBy || null),
                value: value ?? undefined
            }, void 0, false, {
                fileName: "src/elements/QueryPresets/fields/GroupByField/index.tsx",
                lineNumber: 43,
                columnNumber: 7
            }, this)
        ]
    }, void 0, true, {
        fileName: "src/elements/QueryPresets/fields/GroupByField/index.tsx",
        lineNumber: 41,
        columnNumber: 5
    }, this);
};

//# sourceMappingURL=index.js.map