"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useTableFieldContext: ()=>useTableFieldContext,
    TableFieldProvider: ()=>TableFieldProvider,
    useTableFieldProps: ()=>useTableFieldProps,
    TableFieldContext: ()=>TableFieldContext,
    TableFieldResource: ()=>TableFieldResource,
    WithoutTableFieldResource: ()=>WithoutTableFieldResource
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_BlockProvider_js_namespaceObject = require("./BlockProvider.js");
const external_FormBlockProvider_js_namespaceObject = require("./FormBlockProvider.js");
const external_FormFieldProvider_js_namespaceObject = require("./FormFieldProvider.js");
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, external_react_namespaceObject.createContext)({});
TableFieldContext.displayName = 'TableFieldContext';
const InternalTableFieldProvider = (props)=>{
    var _formBlockCtx_updateAssociationValues;
    const { params = {}, showIndex, dragSort, fieldName } = props;
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { resource, service } = (0, external_BlockProvider_js_namespaceObject.useBlockRequestContext)();
    const formBlockCtx = (0, external_FormBlockProvider_js_namespaceObject.useFormBlockContext)();
    const formFieldCtx = (0, external_FormFieldProvider_js_namespaceObject.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, jsx_runtime_namespaceObject.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, external_react_namespaceObject.createContext)(null);
WithoutTableFieldResource.displayName = 'WithoutTableFieldResource';
const TableFieldProvider = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(WithoutTableFieldResource.Provider, {
        value: false,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_BlockProvider_js_namespaceObject.BlockProvider, {
            name: "table-field",
            block: 'TableField',
            ...props,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InternalTableFieldProvider, {
                ...props
            })
        })
    });
const useTableFieldContext = ()=>(0, external_react_namespaceObject.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, schema_namespaceObject.useField)();
    const ctx = useTableFieldContext();
    (0, external_react_namespaceObject.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
            });
        }
    };
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
