/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var userWorkflowTaskStats_exports = {};
__export(userWorkflowTaskStats_exports, {
  listMine: () => listMine
});
module.exports = __toCommonJS(userWorkflowTaskStats_exports);
var import_actions = require("@nocobase/actions");
async function listMine(context, next) {
  const { type, search } = context.action.params;
  const normalizedSearch = typeof search === "string" ? search.trim() : "";
  const page = Math.max(Number.parseInt(String(context.action.params.page ?? 1), 10) || 1, 1);
  const pageSize = Math.min(
    Math.max(Number.parseInt(String(context.action.params.pageSize ?? 200), 10) || 200, 1),
    200
  );
  const pagination = import_actions.utils.pageArgsToLimitArgs(page, pageSize);
  const repository = context.db.getRepository("userWorkflowTaskStats");
  const rows = await repository.find({
    filter: {
      userId: context.state.currentUser.id,
      ...type ? { type } : {},
      all: {
        $gt: 0
      }
    },
    fields: ["workflowKey", "pending", "all"]
  });
  const statsMap = /* @__PURE__ */ new Map();
  for (const row of rows) {
    if (!row.workflowKey) {
      continue;
    }
    const existed = statsMap.get(row.workflowKey) ?? {
      workflowKey: row.workflowKey,
      stats: { pending: 0, all: 0 }
    };
    existed.stats.pending += Number(row.pending) || 0;
    existed.stats.all += Number(row.all) || 0;
    statsMap.set(row.workflowKey, existed);
  }
  const workflowKeys = Array.from(statsMap.keys());
  if (!workflowKeys.length) {
    context.body = {
      rows: [],
      page,
      pageSize,
      hasNext: false
    };
    await next();
    return;
  }
  const workflows = await context.db.getRepository("workflows").find({
    filter: {
      key: workflowKeys,
      current: true,
      ...normalizedSearch ? {
        title: {
          $includes: normalizedSearch
        }
      } : {}
    },
    fields: ["key", "title"],
    sort: ["title"],
    limit: pagination.limit + 1,
    offset: pagination.offset
  });
  const hasNext = workflows.length > pageSize;
  const data = workflows.slice(0, pageSize).map((workflow) => {
    var _a;
    return {
      workflowKey: workflow.key,
      title: workflow.title,
      stats: ((_a = statsMap.get(workflow.key)) == null ? void 0 : _a.stats) ?? { pending: 0, all: 0 }
    };
  });
  context.body = {
    rows: data,
    page,
    pageSize,
    hasNext
  };
  await next();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  listMine
});
