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