import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__ from "./BlockProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__FormBlockProvider_mjs__ from "./FormBlockProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__FormFieldProvider_mjs__ from "./FormFieldProvider.mjs";
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
const TableFieldContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)({});
TableFieldContext.displayName = 'TableFieldContext';
const InternalTableFieldProvider = (props)=>{
    var _formBlockCtx_updateAssociationValues;
    const { params = {}, showIndex, dragSort, fieldName } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { resource, service } = (0, __WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.useBlockRequestContext)();
    const formBlockCtx = (0, __WEBPACK_EXTERNAL_MODULE__FormBlockProvider_mjs__.useFormBlockContext)();
    const formFieldCtx = (0, __WEBPACK_EXTERNAL_MODULE__FormFieldProvider_mjs__.useFormFieldContext)();
    const fullFieldName = formFieldCtx && formFieldCtx.fieldName ? "".concat(formFieldCtx.fieldName, ".").concat(fieldName) : fieldName;
    if (!(null == formBlockCtx ? void 0 : null === (_formBlockCtx_updateAssociationValues = formBlockCtx.updateAssociationValues) || void 0 === _formBlockCtx_updateAssociationValues ? void 0 : _formBlockCtx_updateAssociationValues.includes(fullFieldName))) {
        var _formBlockCtx_updateAssociationValues1;
        null == formBlockCtx || null === (_formBlockCtx_updateAssociationValues1 = formBlockCtx.updateAssociationValues) || void 0 === _formBlockCtx_updateAssociationValues1 || _formBlockCtx_updateAssociationValues1.push(fullFieldName);
    }
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(TableFieldContext.Provider, {
        value: {
            field,
            fieldSchema,
            service,
            resource,
            params,
            showIndex,
            dragSort
        },
        children: props.children
    });
};
class TableFieldResource {
    async list(options) {
        this.field.data = this.field.data || {};
        if (this.field.data.changed) return {
            data: {
                data: this.field.data.dataSource
            }
        };
        if (!this.sourceId) {
            this.field.data.dataSource = [];
            return {
                data: {
                    data: []
                }
            };
        }
        const response = await this.resource.list(options);
        this.field.data.dataSource = response.data.data;
        return {
            data: {
                data: response.data.data
            }
        };
    }
    async get(options) {
        const { filterByTk } = options;
        return {
            data: {
                data: this.field.data.dataSource[filterByTk]
            }
        };
    }
    async create(options) {
        const { values } = options;
        this.field.data.dataSource.push(values);
        this.field.data.changed = true;
    }
    async update(options) {
        const { filterByTk, values } = options;
        this.field.data.dataSource[filterByTk] = values;
        this.field.data.changed = true;
    }
    async destroy(options) {
        let { filterByTk } = options;
        if (!Array.isArray(filterByTk)) filterByTk = [
            filterByTk
        ];
        this.field.data.dataSource = this.field.data.dataSource.filter((item, index)=>!filterByTk.includes(index));
        this.field.data.changed = true;
    }
    constructor(options){
        _define_property(this, "field", void 0);
        _define_property(this, "api", void 0);
        _define_property(this, "sourceId", void 0);
        _define_property(this, "resource", void 0);
        this.field = options.field;
        this.api = options.api;
        this.sourceId = options.sourceId;
        this.resource = this.api.resource(options.resource, this.sourceId);
    }
}
const WithoutTableFieldResource = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
WithoutTableFieldResource.displayName = 'WithoutTableFieldResource';
const TableFieldProvider = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(WithoutTableFieldResource.Provider, {
        value: false,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.BlockProvider, {
            name: "table-field",
            block: 'TableField',
            ...props,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InternalTableFieldProvider, {
                ...props
            })
        })
    });
const useTableFieldContext = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(TableFieldContext);
const useTableFieldProps = ()=>{
    var _ctx_field_data, _ctx_field, _ctx_service_data, _ctx_service, _ctx_service1, _ctx_service2, _ctx_fieldSchema_parent, _ctx_fieldSchema;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const ctx = useTableFieldContext();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        var _ctx_service;
        if (!(null == ctx ? void 0 : null === (_ctx_service = ctx.service) || void 0 === _ctx_service ? void 0 : _ctx_service.loading)) {
            var _ctx_service_data, _ctx_service1, _ctx_field_data, _ctx_field;
            field.value = null == ctx ? void 0 : null === (_ctx_service1 = ctx.service) || void 0 === _ctx_service1 ? void 0 : null === (_ctx_service_data = _ctx_service1.data) || void 0 === _ctx_service_data ? void 0 : _ctx_service_data.data;
            field.data = field.data || {};
            field.data.selectedRowKeys = null == ctx ? void 0 : null === (_ctx_field = ctx.field) || void 0 === _ctx_field ? void 0 : null === (_ctx_field_data = _ctx_field.data) || void 0 === _ctx_field_data ? void 0 : _ctx_field_data.selectedRowKeys;
        }
    }, [
        null == ctx ? void 0 : null === (_ctx_field = ctx.field) || void 0 === _ctx_field ? void 0 : null === (_ctx_field_data = _ctx_field.data) || void 0 === _ctx_field_data ? void 0 : _ctx_field_data.selectedRowKeys,
        null == ctx ? void 0 : null === (_ctx_service = ctx.service) || void 0 === _ctx_service ? void 0 : null === (_ctx_service_data = _ctx_service.data) || void 0 === _ctx_service_data ? void 0 : _ctx_service_data.data,
        null == ctx ? void 0 : null === (_ctx_service1 = ctx.service) || void 0 === _ctx_service1 ? void 0 : _ctx_service1.loading,
        field
    ]);
    return {
        size: 'middle',
        loading: null == ctx ? void 0 : null === (_ctx_service2 = ctx.service) || void 0 === _ctx_service2 ? void 0 : _ctx_service2.loading,
        showIndex: ctx.showIndex,
        dragSort: ctx.dragSort,
        pagination: false,
        required: null == ctx ? void 0 : null === (_ctx_fieldSchema = ctx.fieldSchema) || void 0 === _ctx_fieldSchema ? void 0 : null === (_ctx_fieldSchema_parent = _ctx_fieldSchema.parent) || void 0 === _ctx_fieldSchema_parent ? void 0 : _ctx_fieldSchema_parent.required,
        rowKey: (record)=>{
            var _field_value_indexOf, _field_value;
            return null === (_field_value = field.value) || void 0 === _field_value ? void 0 : null === (_field_value_indexOf = _field_value.indexOf) || void 0 === _field_value_indexOf ? void 0 : _field_value_indexOf.call(_field_value, record);
        },
        onRowSelectionChange (selectedRowKeys) {
            var _ctx_field;
            ctx.field.data = (null == ctx ? void 0 : null === (_ctx_field = ctx.field) || void 0 === _ctx_field ? void 0 : _ctx_field.data) || {};
            ctx.field.data.selectedRowKeys = selectedRowKeys;
        },
        onChange (param) {
            let { current, pageSize } = param;
            ctx.service.run({
                page: current,
                pageSize
            });
        }
    };
};
export { TableFieldContext, TableFieldProvider, TableFieldResource, WithoutTableFieldResource, useTableFieldContext, useTableFieldProps };
