/**
 * 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 __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);
var builder_exports = {};
__export(builder_exports, {
  buildApprovalActionDefaults: () => buildApprovalActionDefaults,
  buildApprovalApproverSurfaceTree: () => buildApprovalApproverSurfaceTree,
  buildApprovalBlockDefaults: () => buildApprovalBlockDefaults,
  buildApprovalFieldTree: () => buildApprovalFieldTree,
  buildApprovalInitiatorSurfaceTree: () => buildApprovalInitiatorSurfaceTree,
  buildApprovalTaskCardSurfaceTree: () => buildApprovalTaskCardSurfaceTree
});
module.exports = __toCommonJS(builder_exports);
var import_utils = require("@nocobase/utils");
var import_lodash = __toESM(require("lodash"));
const APPROVAL_CREATE_NEW_PATTERN_STEP_PARAMS = {
  patternSettings: {
    pattern: {
      pattern: "createNew"
    }
  }
};
function buildFieldInitPayload(params) {
  return import_lodash.default.pickBy(
    {
      dataSourceKey: params.dataSourceKey,
      collectionName: params.collectionName,
      fieldPath: params.fieldPath,
      ...params.associationPathName ? { associationPathName: params.associationPathName } : {}
    },
    (value) => !import_lodash.default.isUndefined(value)
  );
}
function buildApprovalFieldTree(params) {
  var _a, _b;
  if (params.wrapperUse !== "PatternFormItemModel") {
    return null;
  }
  const initPayload = buildFieldInitPayload(params);
  const wrapperStepParams = {
    fieldSettings: {
      init: initPayload
    }
  };
  if (params.filterFormInit) {
    wrapperStepParams.filterFormItemSettings = {
      init: params.filterFormInit
    };
  }
  return {
    wrapperUid: params.uid,
    innerUid: params.innerUid,
    model: {
      uid: params.uid,
      use: "PatternFormItemModel",
      props: import_lodash.default.cloneDeep(params.wrapperProps || {}),
      stepParams: wrapperStepParams,
      subModels: {
        field: {
          uid: params.innerUid,
          use: "PatternFormFieldModel",
          props: import_lodash.default.merge({}, import_lodash.default.cloneDeep(((_a = params.fieldDefaults) == null ? void 0 : _a.props) || {}), import_lodash.default.cloneDeep(params.fieldProps || {})),
          stepParams: import_lodash.default.merge({}, import_lodash.default.cloneDeep(((_b = params.fieldDefaults) == null ? void 0 : _b.stepParams) || {}), {
            fieldSettings: {
              init: initPayload
            },
            fieldBinding: {
              use: params.fieldUse
            }
          })
        }
      }
    }
  };
}
function buildApprovalBlockDefaults(use) {
  switch (String(use || "").trim()) {
    case "ApplyFormModel":
      return {
        stepParams: import_lodash.default.cloneDeep(APPROVAL_CREATE_NEW_PATTERN_STEP_PARAMS),
        subModels: {
          grid: {
            use: "PatternFormGridModel"
          },
          actions: [
            {
              use: "ApplyFormSubmitModel"
            }
          ]
        }
      };
    case "ProcessFormModel":
      return {
        stepParams: import_lodash.default.cloneDeep(APPROVAL_CREATE_NEW_PATTERN_STEP_PARAMS),
        subModels: {
          grid: {
            use: "PatternFormGridModel"
          }
        }
      };
    case "ApprovalDetailsModel":
      return {
        subModels: {
          grid: {
            use: "ApprovalDetailsGridModel"
          }
        }
      };
    default:
      return null;
  }
}
function buildApprovalActionDefaults(use) {
  switch (String(use || "").trim()) {
    case "ApplyFormSubmitModel":
      return {
        props: {
          title: '{{t("Submit")}}',
          type: "primary",
          htmlType: "submit"
        }
      };
    case "ApplyFormSaveDraftModel":
      return {
        props: {
          title: '{{t("Save draft", { ns: "@nocobase/plugin-workflow-approval" })}}',
          type: "default",
          htmlType: "submit"
        }
      };
    case "ApplyFormWithdrawModel":
      return {
        props: {
          title: '{{t("Withdraw", { ns: "@nocobase/plugin-workflow-approval" })}}',
          type: "default",
          htmlType: "submit"
        }
      };
    case "ProcessFormApproveModel":
      return {
        props: {
          title: '{{t("Approve", { ns: "@nocobase/plugin-workflow-approval" })}}',
          type: "primary"
        }
      };
    case "ProcessFormRejectModel":
      return {
        props: {
          title: '{{t("Reject", { ns: "@nocobase/plugin-workflow-approval" })}}',
          type: "default",
          danger: true
        }
      };
    case "ProcessFormReturnModel":
      return {
        props: {
          title: '{{t("Return", { ns: "@nocobase/plugin-workflow-approval" })}}',
          type: "default"
        }
      };
    case "ProcessFormDelegateModel":
      return {
        props: {
          title: '{{t("Delegate", { ns: "@nocobase/plugin-workflow-approval" })}}',
          type: "default"
        }
      };
    case "ProcessFormAddAssigneeModel":
      return {
        props: {
          title: '{{t("Add assignee", { ns: "@nocobase/plugin-workflow-approval" })}}',
          type: "default"
        }
      };
    default:
      return null;
  }
}
function buildApprovalInitiatorSurfaceTree(options) {
  const pageUid = options.pageUid || (0, import_utils.uid)();
  const tabUid = options.tabUid || (0, import_utils.uid)();
  const gridUid = options.gridUid || (0, import_utils.uid)();
  return {
    uid: pageUid,
    async: true,
    subType: "object",
    use: "TriggerChildPageModel",
    sortIndex: 0,
    flowRegistry: {},
    stepParams: {
      pageSettings: {
        general: {
          displayTitle: false,
          enableTabs: false,
          title: `{{t("Initiator's interface", { ns: "workflow" })}}`
        }
      },
      TriggerChildPageSettings: {
        init: {
          dataSourceKey: options.dataSourceKey,
          collectionName: options.collectionName
        }
      }
    },
    subModels: {
      tabs: [
        {
          uid: tabUid,
          parentId: pageUid,
          use: "TriggerChildPageTabModel",
          subKey: "tabs",
          subType: "array",
          sortIndex: 0,
          flowRegistry: {},
          stepParams: {
            pageTabSettings: {
              tab: {
                title: `{{t("Details")}}`
              }
            }
          },
          subModels: {
            grid: {
              uid: gridUid,
              parentId: tabUid,
              async: true,
              use: "TriggerBlockGridModel",
              subKey: "grid",
              subType: "object",
              sortIndex: 0,
              flowRegistry: {},
              filterManager: []
            }
          }
        }
      ]
    }
  };
}
function buildApprovalApproverSurfaceTree(options) {
  const pageUid = options.pageUid || (0, import_utils.uid)();
  const tabUid = options.tabUid || (0, import_utils.uid)();
  const gridUid = options.gridUid || (0, import_utils.uid)();
  return {
    uid: pageUid,
    async: true,
    subType: "object",
    use: "ApprovalChildPageModel",
    sortIndex: 0,
    flowRegistry: {},
    stepParams: {
      pageSettings: {
        general: {
          displayTitle: false,
          enableTabs: false,
          title: `{{t("Approver's interface", { ns: "@nocobase/plugin-workflow-approval" })}}`
        }
      },
      ApprovalChildPageSettings: {
        init: {
          dataSourceKey: options.dataSourceKey,
          collectionName: options.collectionName
        }
      },
      resourceSettings: {
        init: {
          dataSourceKey: options.dataSourceKey,
          collectionName: options.collectionName
        }
      }
    },
    subModels: {
      tabs: [
        {
          uid: tabUid,
          parentId: pageUid,
          use: "ApprovalChildPageTabModel",
          subKey: "tabs",
          subType: "array",
          sortIndex: 0,
          flowRegistry: {},
          stepParams: {
            pageTabSettings: {
              tab: {
                title: `{{t("Details")}}`
              }
            }
          },
          subModels: {
            grid: {
              uid: gridUid,
              parentId: tabUid,
              async: true,
              use: "ApprovalBlockGridModel",
              subKey: "grid",
              subType: "object",
              sortIndex: 0,
              flowRegistry: {},
              filterManager: []
            }
          }
        }
      ]
    }
  };
}
function buildApprovalTaskCardSurfaceTree(options) {
  const detailsUid = options.detailsUid || (0, import_utils.uid)();
  const gridUid = options.gridUid || (0, import_utils.uid)();
  return {
    uid: detailsUid,
    async: true,
    subType: "object",
    use: options.detailsUse,
    sortIndex: 0,
    flowRegistry: {},
    stepParams: {
      resourceSettings: {
        init: {
          dataSourceKey: options.dataSourceKey,
          collectionName: options.collectionName
        }
      },
      detailsSettings: {
        layout: {
          layout: "horizontal"
        }
      }
    },
    subModels: {
      grid: {
        uid: gridUid,
        parentId: detailsUid,
        use: options.gridUse,
        subKey: "grid",
        subType: "object",
        sortIndex: 0,
        flowRegistry: {}
      }
    }
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildApprovalActionDefaults,
  buildApprovalApproverSurfaceTree,
  buildApprovalBlockDefaults,
  buildApprovalFieldTree,
  buildApprovalInitiatorSurfaceTree,
  buildApprovalTaskCardSurfaceTree
});
