import react from "react";
import { isArr } from "@tachybase/schema";
import { getDefaultFormat, str2moment } from "@tego/client";
import { Tag } from "antd";
import { useCollectionManager_deprecated } from "../../../collection-manager/index.mjs";
const useLabelUiSchemaV2 = ()=>{
    const { getCollectionJoinField } = useCollectionManager_deprecated();
    return (collectionName, label)=>{
        if (!collectionName) return;
        const labelField = getCollectionJoinField("".concat(collectionName, ".").concat(label));
        return null == labelField ? void 0 : labelField.uiSchema;
    };
};
const useLabelUiSchema = (collectionName, label)=>{
    const { getCollectionJoinField } = useCollectionManager_deprecated();
    if (!collectionName) return;
    const labelField = getCollectionJoinField("".concat(collectionName, ".").concat(label));
    return null == labelField ? void 0 : labelField.uiSchema;
};
const getDatePickerLabels = (props)=>{
    const format = getDefaultFormat(props);
    const m = str2moment(props.value, props);
    const labels = m && m.isValid() ? m.format(format) : props.value;
    return isArr(labels) ? labels.join('~') : labels;
};
const toArr = (v)=>{
    if (!v) return [];
    return Array.isArray(v) ? v : [
        v
    ];
};
const getLabelFormatValue = function(labelUiSchema, value) {
    let isTag = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : false;
    const options = null == labelUiSchema ? void 0 : labelUiSchema.enum;
    if (Array.isArray(options) && value) {
        const values = toArr(value).map((val)=>{
            const opt = options.find((option)=>option.value === val);
            if (isTag) return react.createElement(Tag, {
                color: null == opt ? void 0 : opt.color
            }, null == opt ? void 0 : opt.label);
            return null == opt ? void 0 : opt.label;
        });
        return isTag ? values : values.join(', ');
    }
    switch(null == labelUiSchema ? void 0 : labelUiSchema['x-component']){
        case 'DatePicker':
            return getDatePickerLabels({
                ...null == labelUiSchema ? void 0 : labelUiSchema['x-component-props'],
                value
            });
        default:
            return value;
    }
};
const getTabFormatValue = (labelUiSchema, value, tagColor)=>{
    const options = null == labelUiSchema ? void 0 : labelUiSchema.enum;
    if (Array.isArray(options) && value) {
        const values = toArr(value).map((val)=>{
            const opt = options.find((option)=>option.value === val);
            return react.createElement(Tag, {
                color: tagColor || (null == opt ? void 0 : opt.color)
            }, null == opt ? void 0 : opt.label);
        });
        return values;
    }
    switch(null == labelUiSchema ? void 0 : labelUiSchema['x-component']){
        case 'DatePicker':
            return react.createElement(Tag, {
                color: tagColor
            }, getDatePickerLabels({
                ...null == labelUiSchema ? void 0 : labelUiSchema['x-component-props'],
                value
            }));
        default:
            return react.createElement(Tag, {
                color: tagColor
            }, value);
    }
};
function flatData(data) {
    const newArr = [];
    for(let i = 0; i < data.length; i++){
        const children = data[i]['children'];
        if (Array.isArray(children)) newArr.push(...flatData(children));
        newArr.push({
            ...data[i]
        });
    }
    return newArr;
}
function isShowFilePicker(labelUiSchema) {
    return (null == labelUiSchema ? void 0 : labelUiSchema['x-component']) === 'Preview';
}
function isSubMode(field) {
    var _field_xcomponentprops, _field_componentProps;
    const mode = (null == (_field_xcomponentprops = field['x-component-props']) ? void 0 : _field_xcomponentprops.mode) || (null == (_field_componentProps = field.componentProps) ? void 0 : _field_componentProps.mode);
    return [
        'Nester',
        'SubTable',
        'PopoverNester'
    ].includes(mode);
}
export { flatData, getDatePickerLabels, getLabelFormatValue, getTabFormatValue, isShowFilePicker, isSubMode, useLabelUiSchema, useLabelUiSchemaV2 };
