var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/dataSourceComponents/dataSourceTable/hooks/useFormat.ts
var useFormat_exports = {};
__export(useFormat_exports, {
  default: () => useFormat_default
});
module.exports = __toCommonJS(useFormat_exports);
var import_ahooks = require("ahooks");
var import_utils = require("../../fields/utils");
var import_utils2 = require("@pisell/utils");
var import_dayjs = __toESM(require("dayjs"));
var 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"
];
var formatFilter = (filterValue, fields) => {
  const conditions = [];
  if (!filterValue || !(fields == null ? void 0 : fields.length)) {
    return JSON.stringify({});
  }
  if ((0, import_utils2.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 _a;
    if (key === "_keywords" || value === void 0 || value === null || value === "" || (0, import_utils2.isArr)(value) && value.length === 0) {
      return;
    }
    if ((0, import_utils2.isString)(value) && filterOperatorArr.some((item) => value.includes(item))) {
      if ((0, import_utils2.isJson)(value)) {
        conditions.push({ [key]: JSON.parse(value) });
      }
      return;
    }
    const field = fieldsMap[key];
    if (!((_a = field == null ? void 0 : field.uiSchema) == null ? void 0 : _a["x-component"])) {
      return;
    }
    const filterFunc = import_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);
};
var formatSort = (sortValue) => {
  if (!sortValue)
    return void 0;
  const [field, order] = sortValue.split(":");
  const prefix = order === "desc" ? "-" : "";
  return [`${prefix}${field}`];
};
var formatCreateUpdateParamsUtils = (params, fields) => {
  if (!params || !(fields == null ? void 0 : fields.length)) {
    return params;
  }
  const formattedParams = { ...params };
  fields.forEach((field) => {
    var _a, _b, _c, _d;
    const fieldName = field.name;
    const value = params[fieldName];
    if (((_a = field.uiSchema) == null ? void 0 : _a["x-component"]) === "TimePicker" && value) {
      if (import_dayjs.default.isDayjs(value)) {
        formattedParams[fieldName] = value.format("HH:mm:ss");
      } else if ((0, import_utils2.isString)(value)) {
        const timeArr = value.split(":");
        const hours = ((_b = timeArr[0]) == null ? void 0 : _b.padStart(2, "0")) || "00";
        const minutes = ((_c = timeArr[1]) == null ? void 0 : _c.padStart(2, "0")) || "00";
        const seconds = ((_d = timeArr[2]) == null ? void 0 : _d.padStart(2, "0")) || "00";
        formattedParams[fieldName] = `${hours}:${minutes}:${seconds}`;
      }
    }
  });
  return formattedParams;
};
var customApiFormatFilter = (filter) => {
  return {
    ...filter
  };
};
var useFormat = ({ dataSourceKey, fields }) => {
  const formatListResult = (0, import_ahooks.useMemoizedFn)(
    (res) => {
      if (dataSourceKey === "pisell2-api" && "list" in res && "page" in res) {
        return {
          data: res.list,
          count: res.count,
          page: res.page,
          pageSize: res.size
        };
      }
      if (dataSourceKey === "mypisell-api" && "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;
    }
  );
  const formatListParams = (0, import_ahooks.useMemoizedFn)((params) => {
    if (!dataSourceKey || ["pisell2", "main"].includes(dataSourceKey)) {
      return {
        ...params,
        filter: formatFilter(params.filter, fields),
        sort: formatSort(params.sort)
      };
    }
    if (dataSourceKey === "pisell2-api") {
      return {
        page: (params == null ? void 0 : params.page) || 1,
        size: (params == null ? void 0 : params.pageSize) || 10,
        ...customApiFormatFilter(params.filter || {})
      };
    }
    if (dataSourceKey === "mypisell-api") {
      return {
        skip: (params == null ? void 0 : params.page) || 1,
        num: (params == null ? void 0 : params.pageSize) || 10,
        ...customApiFormatFilter(params.filter || {})
      };
    }
    return params;
  });
  const formatCreateUpdateParams = (0, import_ahooks.useMemoizedFn)((params) => {
    if (!dataSourceKey) {
      return formatCreateUpdateParamsUtils(params, fields);
    }
    return params;
  });
  return {
    formatListResult,
    formatListParams,
    formatCreateUpdateParams
  };
};
var useFormat_default = useFormat;
