import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
const useLabelUiSchemaV2 = ()=>{
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.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 } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    if (!collectionName) return;
    const labelField = getCollectionJoinField("".concat(collectionName, ".").concat(label));
    return null == labelField ? void 0 : labelField.uiSchema;
};
const getDatePickerLabels = (props)=>{
    const format = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.getDefaultFormat)(props);
    const m = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.str2moment)(props.value, props);
    const labels = m && m.isValid() ? m.format(format) : props.value;
    return (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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];
    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 __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE_antd__.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 __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE_antd__.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 __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE_antd__.Tag, {
                color: tagColor
            }, getDatePickerLabels({
                ...null == labelUiSchema ? void 0 : labelUiSchema['x-component-props'],
                value
            }));
        default:
            return __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE_antd__.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 ? void 0 : _field_xcomponentprops.mode) || (null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : _field_componentProps.mode);
    return [
        'Nester',
        'SubTable',
        'PopoverNester'
    ].includes(mode);
}
export { flatData, getDatePickerLabels, getLabelFormatValue, getTabFormatValue, isShowFilePicker, isSubMode, useLabelUiSchema, useLabelUiSchemaV2 };
