const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_utils = require("../../fields/utils.js");
let _pisell_utils = require("@pisell/utils");
let ahooks = require("ahooks");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
//#region src/components/dataSourceComponents/dataSourceTable/hooks/useFormat.ts
const filterOperatorArr = [
	"$eq",
	"$ne",
	"$is",
	"$not",
	"$col",
	"$in",
	"$notIn",
	"$empty",
	"$notEmpty",
	"$and",
	"$or",
	"$isFalsy",
	"$isTruly",
	"$gt",
	"$gte",
	"$lt",
	"$lte",
	"$between",
	"$notBetween",
	"$includes",
	"$notIncludes",
	"$startsWith",
	"$notStatsWith",
	"$endsWith",
	"$notEndsWith",
	"$like",
	"$notLike",
	"$iLike",
	"$notILike",
	"$regexp",
	"$notRegexp",
	"$iRegexp",
	"$notIRegexp",
	"$dateOn",
	"$dateNotOn",
	"$dateBefore",
	"$dateNotBefore",
	"$dateAfter",
	"$dateNotAfter",
	"$match",
	"$notMatch",
	"$anyOf",
	"$noneOf",
	"$arrayEmpty",
	"$arrayNotEmpty",
	"$exists",
	"$notExists"
];
const formatFilter = (filterValue, fields) => {
	const conditions = [];
	if (!filterValue || !(fields === null || fields === void 0 ? void 0 : fields.length)) return JSON.stringify({});
	if ((0, _pisell_utils.isString)(filterValue)) return filterValue;
	if (filterValue._keywords) {
		const keywordFields = fields.filter((item) => [
			"string",
			"number",
			"text"
		].includes(item.type)).map((item) => ({ [item.name]: { $includes: filterValue._keywords } }));
		if (keywordFields.length > 0) conditions.push({ $or: keywordFields });
	}
	const fieldsMap = fields.reduce((acc, field) => {
		if (field.name) acc[field.name] = field;
		return acc;
	}, {});
	const normalConditions = [];
	Object.entries(filterValue).forEach(([key, value]) => {
		var _field$uiSchema;
		if (key === "_keywords" || value === void 0 || value === null || value === "" || (0, _pisell_utils.isArr)(value) && value.length === 0) return;
		if ((0, _pisell_utils.isString)(value) && filterOperatorArr.some((item) => value.includes(item))) {
			if ((0, _pisell_utils.isJson)(value)) conditions.push({ [key]: JSON.parse(value) });
			return;
		}
		if (key && (0, _pisell_utils.isPlainObject)(value)) {
			Object.entries(value).forEach(([subKey, subVal]) => {
				if (subVal) normalConditions.push({ [`${key}.${subKey}`]: { $includes: subVal } });
			});
			return;
		}
		const field = fieldsMap[key];
		if (!(field === null || field === void 0 || (_field$uiSchema = field.uiSchema) === null || _field$uiSchema === void 0 ? void 0 : _field$uiSchema["x-component"])) return;
		const filterFunc = require_utils.formFieldFilterFuncMap[field.uiSchema["x-component"]];
		if (!filterFunc) return;
		const condition = filterFunc(value, field);
		if (condition) normalConditions.push({ [key]: condition });
	});
	if (normalConditions.length > 0) conditions.push({ $and: normalConditions });
	const filterCondition = conditions.length ? { $and: conditions } : {};
	return JSON.stringify(filterCondition);
};
const formatSort = (sortValue) => {
	if (!sortValue) return void 0;
	const [field, order] = sortValue.split(":");
	return [`${order === "desc" ? "-" : ""}${field}`];
};
const formatCreateUpdateParamsUtils = (params, fields) => {
	if (!params || !(fields === null || fields === void 0 ? void 0 : fields.length)) return params;
	const formattedParams = require_objectSpread2._objectSpread2({}, params);
	fields.forEach((field) => {
		var _field$uiSchema2;
		const fieldName = field.name;
		const value = params[fieldName];
		if (((_field$uiSchema2 = field.uiSchema) === null || _field$uiSchema2 === void 0 ? void 0 : _field$uiSchema2["x-component"]) === "TimePicker" && value) {
			if (dayjs.default.isDayjs(value)) formattedParams[fieldName] = value.format("HH:mm:ss");
			else if ((0, _pisell_utils.isString)(value)) {
				var _timeArr$, _timeArr$2, _timeArr$3;
				const timeArr = value.split(":");
				formattedParams[fieldName] = `${((_timeArr$ = timeArr[0]) === null || _timeArr$ === void 0 ? void 0 : _timeArr$.padStart(2, "0")) || "00"}:${((_timeArr$2 = timeArr[1]) === null || _timeArr$2 === void 0 ? void 0 : _timeArr$2.padStart(2, "0")) || "00"}:${((_timeArr$3 = timeArr[2]) === null || _timeArr$3 === void 0 ? void 0 : _timeArr$3.padStart(2, "0")) || "00"}`;
			}
		}
	});
	return formattedParams;
};
const customApiFormatFilter = (filter) => {
	return require_objectSpread2._objectSpread2({}, filter);
};
const useFormat = ({ dataSourceKey, fields }) => {
	return {
		formatListResult: (0, ahooks.useMemoizedFn)((res) => {
			if (dataSourceKey === "pisell2-api") {
				if (Array.isArray(res)) return {
					data: res,
					count: res.length,
					page: 1,
					pageSize: res.length
				};
				else if ("list" in res && "page" in res) return {
					data: res.list,
					count: res.count,
					page: res.page,
					pageSize: res.size
				};
			}
			if (dataSourceKey === "mypisell-api") {
				if (Array.isArray(res)) return {
					data: res,
					count: res.length,
					page: 1,
					pageSize: res.length
				};
				else if ("list" in res && "skip" in res) {
					const skip = typeof res.skip === "number" ? res.skip : 1;
					return {
						data: res.list,
						count: res.count,
						page: skip - 1 || 1,
						pageSize: res.size
					};
				}
			}
			if ("meta" in res) return {
				data: res.data,
				count: res.meta.count,
				page: res.meta.page,
				pageSize: res.meta.pageSize
			};
			return res;
		}),
		formatListParams: (0, ahooks.useMemoizedFn)((params) => {
			if (!dataSourceKey || ["pisell2", "main"].includes(dataSourceKey)) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, params), {}, {
				filter: formatFilter(params.filter, fields),
				sort: formatSort(params.sort)
			});
			if (dataSourceKey === "pisell2-api") return require_objectSpread2._objectSpread2({
				page: (params === null || params === void 0 ? void 0 : params.page) || 1,
				size: (params === null || params === void 0 ? void 0 : params.pageSize) || 10
			}, customApiFormatFilter(params.filter || {}));
			if (dataSourceKey === "mypisell-api") return require_objectSpread2._objectSpread2({
				skip: (params === null || params === void 0 ? void 0 : params.page) || 1,
				num: (params === null || params === void 0 ? void 0 : params.pageSize) || 10
			}, customApiFormatFilter(params.filter || {}));
			return params;
		}),
		formatCreateUpdateParams: (0, ahooks.useMemoizedFn)((params) => {
			if (!dataSourceKey) return formatCreateUpdateParamsUtils(params, fields);
			return params;
		})
	};
};
//#endregion
exports.default = useFormat;
