import { jsx } from "react/jsx-runtime";
import "react";
import { connect, mapProps, mapReadPretty, toArr, useField } from "@tachybase/schema";
import { LoadingOutlined } from "@ant-design/icons";
import { Cascader, Space } from "antd";
import { isBoolean, omit } from "lodash";
import { useRequest } from "../../../api-client/index.mjs";
import { defaultFieldNames } from "./defaultFieldNames.mjs";
import { ReadPretty } from "./ReadPretty.mjs";
const useDefDataSource = (options)=>{
    const field = useField();
    return useRequest(()=>Promise.resolve({
            data: field.dataSource || []
        }), options);
};
const useDefLoadData = (props)=>null == props ? void 0 : props.loadData;
const Cascader_Cascader = connect((props)=>{
    const field = useField();
    const { value, onChange, labelInValue, useDataSource = useDefDataSource, useLoadData = useDefLoadData, changeOnSelectLast, changeOnSelect, maxLevel, ...others } = props;
    const fieldNames = {
        ...defaultFieldNames,
        ...props.fieldNames
    };
    const loadData = useLoadData(props);
    const { loading, run } = useDataSource({
        onSuccess (data) {
            field.dataSource = (null == data ? void 0 : data.data) || [];
        }
    });
    const toValue = ()=>toArr(value).map((item)=>{
            if ('object' == typeof item) return item[fieldNames.value];
            return item;
        });
    const displayRender = (labels, selectedOptions)=>/*#__PURE__*/ jsx(Space, {
            split: '/',
            children: labels.map((label, index)=>{
                if (selectedOptions[index]) return /*#__PURE__*/ jsx("span", {
                    children: label
                }, index);
                const item = toArr(value).filter(Boolean).find((item)=>item[fieldNames.value] === label);
                return /*#__PURE__*/ jsx("span", {
                    children: (null == item ? void 0 : item[fieldNames.label]) || label
                }, index);
            })
        });
    const handelDropDownVisible = (value)=>{
        var _field_dataSource;
        if (value && !(null == (_field_dataSource = field.dataSource) ? void 0 : _field_dataSource.length)) run();
    };
    return /*#__PURE__*/ jsx(Cascader, {
        loading: loading,
        ...others,
        options: field.dataSource,
        loadData: loadData,
        changeOnSelect: isBoolean(changeOnSelectLast) ? !changeOnSelectLast : changeOnSelect,
        value: toValue(),
        fieldNames: fieldNames,
        displayRender: displayRender,
        onDropdownVisibleChange: handelDropDownVisible,
        onChange: (value, selectedOptions)=>{
            value && labelInValue ? onChange(selectedOptions.map((option)=>omit(option, [
                    fieldNames.children
                ]))) : onChange(value);
        }
    });
}, mapProps({
    dataSource: 'options'
}, (props, field)=>({
        ...props,
        suffixIcon: (null == field ? void 0 : field['loading']) || (null == field ? void 0 : field['validating']) ? /*#__PURE__*/ jsx(LoadingOutlined, {}) : props.suffixIcon
    })), mapReadPretty(ReadPretty));
const cascader_Cascader = Cascader_Cascader;
export { Cascader_Cascader as Cascader, cascader_Cascader as default };
