/**
 * 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 default_block_actions_exports = {};
__export(default_block_actions_exports, {
  FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY: () => FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY,
  getFlowSurfaceDefaultBlockActions: () => getFlowSurfaceDefaultBlockActions,
  mergeFlowSurfaceDefaultBlockActions: () => mergeFlowSurfaceDefaultBlockActions
});
module.exports = __toCommonJS(default_block_actions_exports);
var import_lodash = __toESM(require("lodash"));
var import_defaults = require("./blueprint/defaults");
var import_template_reference = require("./template-reference");
const FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY = "__flowSurfaceAutoSaveDefaultPopupTemplate";
const FLOW_SURFACE_DEFAULT_BLOCK_ACTIONS = {
  table: [
    { type: "filter", scope: "actions" },
    { type: "refresh", scope: "actions" },
    { type: "bulkDelete", scope: "actions" },
    {
      type: "addNew",
      scope: "actions",
      popup: {
        tryTemplate: true,
        [FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY]: true
      }
    },
    {
      type: "view",
      scope: "recordActions",
      popup: {
        tryTemplate: true,
        defaultType: "view",
        [FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY]: true
      }
    },
    {
      type: "edit",
      scope: "recordActions",
      popup: {
        tryTemplate: true,
        defaultType: "edit",
        [FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY]: true
      }
    },
    { type: "delete", scope: "recordActions" }
  ],
  list: [
    { type: "filter", scope: "actions" },
    { type: "refresh", scope: "actions" },
    {
      type: "addNew",
      scope: "actions",
      popup: {
        tryTemplate: true,
        [FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY]: true
      }
    }
  ],
  gridCard: [
    { type: "filter", scope: "actions" },
    { type: "refresh", scope: "actions" },
    {
      type: "addNew",
      scope: "actions",
      popup: {
        tryTemplate: true,
        [FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY]: true
      }
    }
  ],
  calendar: [
    { type: "filter", scope: "actions" },
    { type: "turnPages", scope: "actions" },
    { type: "selectView", scope: "actions" },
    { type: "refresh", scope: "actions" },
    {
      type: "addNew",
      scope: "actions",
      popup: {
        tryTemplate: true,
        [FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY]: true
      }
    }
  ],
  kanban: [
    { type: "filter", scope: "actions" },
    { type: "refresh", scope: "actions" },
    {
      type: "addNew",
      scope: "actions",
      popup: {
        tryTemplate: true,
        [FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY]: true
      }
    }
  ],
  createForm: [{ type: "submit", scope: "actions" }],
  editForm: [{ type: "submit", scope: "actions" }],
  details: [
    {
      type: "edit",
      scope: "recordActions",
      popup: {
        tryTemplate: true,
        defaultType: "edit",
        [FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY]: true
      }
    }
  ],
  comments: [
    { type: "edit", scope: "recordActions" },
    { type: "delete", scope: "recordActions" },
    { type: "quoteReply", scope: "recordActions" }
  ],
  recordHistory: [
    { type: "filter", scope: "actions" },
    { type: "refresh", scope: "actions" },
    { type: "expandAll", scope: "actions" },
    { type: "collapseAll", scope: "actions" }
  ]
};
function cloneDefaultActionDescriptor(descriptor) {
  return {
    ...descriptor,
    ...descriptor.popup ? { popup: import_lodash.default.cloneDeep(descriptor.popup) } : {}
  };
}
function normalizeActionType(value) {
  return typeof value === "string" ? value.trim() : String(value || "").trim();
}
function resolveDefaultBlockActions(blockType, template) {
  const normalizedBlockType = normalizeActionType(blockType);
  if (!normalizedBlockType || (0, import_template_reference.hasFlowSurfaceTemplateDocument)(template)) {
    return [];
  }
  return (FLOW_SURFACE_DEFAULT_BLOCK_ACTIONS[normalizedBlockType] || []).map(cloneDefaultActionDescriptor);
}
function shouldMergeDefaultPopup(popup) {
  if (!import_lodash.default.isPlainObject(popup)) {
    return true;
  }
  if (Object.prototype.hasOwnProperty.call(popup, "tryTemplate")) {
    return false;
  }
  const keys = Object.keys(popup);
  if (!keys.length) {
    return true;
  }
  return keys.every(
    (key) => ["mode", "size", "title", "defaultType", "tryTemplate", import_defaults.FLOW_SURFACE_APPLY_BLUEPRINT_POPUP_DEFAULTS_KEY].includes(
      key
    )
  );
}
function mergeExplicitActionWithDescriptor(item, descriptor) {
  if (!descriptor.popup || !shouldMergeDefaultPopup(item == null ? void 0 : item.popup)) {
    return item;
  }
  return {
    ...item,
    popup: import_lodash.default.isPlainObject(item == null ? void 0 : item.popup) ? {
      ...import_lodash.default.cloneDeep(descriptor.popup),
      ...import_lodash.default.cloneDeep(item.popup)
    } : import_lodash.default.cloneDeep(descriptor.popup)
  };
}
function mergeDefaultActionList(existing, descriptors, createAction) {
  const existingList = Array.isArray(existing) ? existing : [];
  if (!descriptors.length) {
    return [...existingList];
  }
  const defaultTypes = new Set(descriptors.map((descriptor) => descriptor.type));
  const explicitByType = /* @__PURE__ */ new Map();
  const extras = [];
  for (const item of existingList) {
    const type = normalizeActionType(item == null ? void 0 : item.type);
    if (!type || !defaultTypes.has(type)) {
      extras.push(item);
      continue;
    }
    if (!explicitByType.has(type)) {
      explicitByType.set(type, item);
      continue;
    }
    extras.push(item);
  }
  return [
    ...descriptors.map((descriptor) => {
      const explicit = explicitByType.get(descriptor.type);
      return explicit ? mergeExplicitActionWithDescriptor(explicit, descriptor) : createAction(descriptor);
    }),
    ...extras
  ];
}
function getFlowSurfaceDefaultBlockActions(input) {
  return resolveDefaultBlockActions(input.blockType, input.template);
}
function mergeFlowSurfaceDefaultBlockActions(input) {
  const descriptors = resolveDefaultBlockActions(input.blockType, input.template);
  const actionDescriptors = descriptors.filter((descriptor) => descriptor.scope === "actions");
  const recordActionDescriptors = input.mergeRecordActionDefaults === false ? [] : descriptors.filter((descriptor) => descriptor.scope === "recordActions");
  return {
    actions: mergeDefaultActionList(input.actions, actionDescriptors, input.createAction),
    recordActions: mergeDefaultActionList(input.recordActions, recordActionDescriptors, input.createAction)
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FLOW_SURFACE_INTERNAL_AUTO_SAVE_DEFAULT_POPUP_TEMPLATE_KEY,
  getFlowSurfaceDefaultBlockActions,
  mergeFlowSurfaceDefaultBlockActions
});
