'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { getTranslation } from '@payloadcms/translations';
import { toWords } from 'payload/shared';
import React from 'react';
import { FieldLabel } from '../../../../fields/FieldLabel/index.js';
import { useField } from '../../../../forms/useField/index.js';
import { useConfig } from '../../../../providers/Config/index.js';
import { useListQuery } from '../../../../providers/ListQuery/index.js';
import { useTranslation } from '../../../../providers/Translation/index.js';
import { Pill } from '../../../Pill/index.js';
import './index.scss';
/** @todo: improve this */ const transformWhereToNaturalLanguage = (where, collectionLabel)=>{
    if (!where) {
        return null;
    }
    const renderCondition = (condition)=>{
        if (!condition || typeof condition !== 'object') {
            return 'No where query';
        }
        const key = Object.keys(condition)[0];
        if (!key || !condition[key] || typeof condition[key] !== 'object') {
            return 'No where query';
        }
        const operator = Object.keys(condition[key])[0];
        const operatorValue = condition[key][operator];
        // Format value - ideally would use field schema for proper typing
        const formatValue = (val)=>{
            if (typeof val === 'object' && val != null) {
                try {
                    return new Date(val).toLocaleDateString();
                } catch  {
                    return 'Unknown error has occurred';
                }
            }
            return val?.toString() ?? '';
        };
        const value = Array.isArray(operatorValue) ? operatorValue.map(formatValue).join(', ') : formatValue(operatorValue);
        return /*#__PURE__*/ _jsxDEV(Pill, {
            pillStyle: "always-white",
            size: "small",
            children: [
                /*#__PURE__*/ _jsxDEV("b", {
                    children: toWords(key)
                }, void 0, false, {
                    fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                    lineNumber: 57,
                    columnNumber: 9
                }, this),
                " ",
                operator,
                " ",
                /*#__PURE__*/ _jsxDEV("b", {
                    children: toWords(value)
                }, void 0, false, {
                    fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                    lineNumber: 57,
                    columnNumber: 42
                }, this)
            ]
        }, void 0, true, {
            fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
            lineNumber: 56,
            columnNumber: 7
        }, this);
    };
    const renderWhere = (where, collectionLabel)=>{
        if (where.or && where.or.length > 0) {
            return /*#__PURE__*/ _jsxDEV("div", {
                className: "or-condition",
                children: where.or.map((orCondition, orIndex)=>/*#__PURE__*/ _jsxDEV(React.Fragment, {
                        children: orCondition.and && orCondition.and.length > 0 ? /*#__PURE__*/ _jsxDEV("div", {
                            className: "and-condition",
                            children: [
                                orIndex === 0 && /*#__PURE__*/ _jsxDEV("span", {
                                    className: "label",
                                    children: `Filter ${collectionLabel} where `
                                }, void 0, false, {
                                    fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                                    lineNumber: 71,
                                    columnNumber: 21
                                }, this),
                                orIndex > 0 && /*#__PURE__*/ _jsxDEV("span", {
                                    className: "label",
                                    children: " or "
                                }, void 0, false, {
                                    fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                                    lineNumber: 73,
                                    columnNumber: 35
                                }, this),
                                orCondition.and.map((andCondition, andIndex)=>/*#__PURE__*/ _jsxDEV(React.Fragment, {
                                        children: [
                                            renderCondition(andCondition),
                                            andIndex < orCondition.and.length - 1 && /*#__PURE__*/ _jsxDEV("span", {
                                                className: "label",
                                                children: " and "
                                            }, void 0, false, {
                                                fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                                                lineNumber: 78,
                                                columnNumber: 25
                                            }, this)
                                        ]
                                    }, andIndex, true, {
                                        fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                                        lineNumber: 75,
                                        columnNumber: 21
                                    }, this))
                            ]
                        }, void 0, true, {
                            fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                            lineNumber: 69,
                            columnNumber: 17
                        }, this) : renderCondition(orCondition)
                    }, orIndex, false, {
                        fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                        lineNumber: 67,
                        columnNumber: 13
                    }, this))
            }, void 0, false, {
                fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                lineNumber: 65,
                columnNumber: 9
            }, this);
        }
        return renderCondition(where);
    };
    return renderWhere(where, collectionLabel);
};
export const QueryPresetsWhereField = ({ field: { label, required } })=>{
    const { path, value } = useField();
    const { collectionSlug } = useListQuery();
    const { getEntityConfig } = useConfig();
    const collectionConfig = getEntityConfig({
        collectionSlug
    });
    const { i18n } = useTranslation();
    return /*#__PURE__*/ _jsxDEV("div", {
        className: "field-type query-preset-where-field",
        children: [
            /*#__PURE__*/ _jsxDEV(FieldLabel, {
                as: "h3",
                label: label,
                path: path,
                required: required
            }, void 0, false, {
                fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                lineNumber: 111,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV("div", {
                className: "value-wrapper",
                children: value ? transformWhereToNaturalLanguage(value, getTranslation(collectionConfig?.labels?.plural, i18n)) : 'No where query'
            }, void 0, false, {
                fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
                lineNumber: 112,
                columnNumber: 7
            }, this)
        ]
    }, void 0, true, {
        fileName: "src/elements/QueryPresets/fields/WhereField/index.tsx",
        lineNumber: 110,
        columnNumber: 5
    }, this);
};

//# sourceMappingURL=index.js.map