/**
 * 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 value_expr_exports = {};
__export(value_expr_exports, {
  barePathToVariableExpression: () => barePathToVariableExpression,
  compileReactionFilter: () => compileReactionFilter,
  compileReactionValueExpr: () => compileReactionValueExpr,
  normalizeReactionFilter: () => normalizeReactionFilter,
  normalizeReactionPath: () => normalizeReactionPath,
  normalizeReactionValueExpr: () => normalizeReactionValueExpr
});
module.exports = __toCommonJS(value_expr_exports);
var import_lodash = __toESM(require("lodash"));
var import_errors = require("../errors");
var import_errors2 = require("./errors");
var import_utils = require("./utils");
const EMPTY_CONDITION = Object.freeze({ logic: "$and", items: [] });
function normalizeBarePath(path, label) {
  if (typeof path === "string" && (0, import_utils.isVariableExpression)(path)) {
    const extracted = (0, import_utils.extractPropertyPath)(path);
    if (Array.isArray(extracted)) {
      return extracted.join(".");
    }
  }
  const normalized = String(path ?? "").trim().split(".").map((segment) => segment.trim()).filter(Boolean).join(".");
  if (!normalized) {
    (0, import_errors.throwBadRequest)(`flowSurfaces reaction ${label} cannot be empty`, import_errors2.FLOW_SURFACE_REACTION_INVALID_CONDITION_PATH);
  }
  return normalized;
}
function barePathToVariableExpression(path, label = "path") {
  const normalized = normalizeBarePath(path, label);
  return (0, import_utils.formatPathToVariable)(
    normalized.split(".").map((segment) => segment.trim()).filter(Boolean)
  );
}
function normalizeReactionValueExpr(input) {
  if (!import_lodash.default.isPlainObject(input) || !Object.prototype.hasOwnProperty.call(input, "source")) {
    if (typeof input === "string" && (0, import_utils.isVariableExpression)(input)) {
      return {
        source: "path",
        path: normalizeBarePath(input, "value.path")
      };
    }
    if (import_lodash.default.isPlainObject(input) && Object.prototype.hasOwnProperty.call(input, "code")) {
      const normalizedRunjs = (0, import_utils.normalizeRunJSValue)({
        code: String(input.code ?? ""),
        version: input.version == null ? void 0 : String(input.version)
      });
      return {
        source: "runjs",
        code: normalizedRunjs.code,
        version: normalizedRunjs.version
      };
    }
    return {
      source: "literal",
      value: import_lodash.default.cloneDeep(input)
    };
  }
  const source = String(input.source || "").trim();
  if (source === "literal") {
    return {
      source: "literal",
      value: import_lodash.default.cloneDeep(input.value)
    };
  }
  if (source === "path") {
    return {
      source: "path",
      path: normalizeBarePath(input.path, "value.path")
    };
  }
  if (source === "runjs") {
    const normalizedRunjs = (0, import_utils.normalizeRunJSValue)({
      code: String(input.code ?? ""),
      version: input.version == null ? void 0 : String(input.version)
    });
    return {
      source: "runjs",
      code: normalizedRunjs.code,
      version: normalizedRunjs.version
    };
  }
  (0, import_errors.throwBadRequest)(`flowSurfaces reaction value.source '${source}' is unsupported`);
}
function compileReactionValueExpr(input) {
  const normalized = normalizeReactionValueExpr(input);
  if (normalized.source === "literal") {
    return import_lodash.default.cloneDeep(normalized.value);
  }
  if (normalized.source === "path") {
    return barePathToVariableExpression(normalized.path, "value.path");
  }
  const normalizedRunjs = (0, import_utils.normalizeRunJSValue)({
    code: normalized.code,
    version: normalized.version
  });
  return {
    code: normalizedRunjs.code,
    version: normalizedRunjs.version
  };
}
function compileConditionNode(input) {
  if (Array.isArray(input)) {
    return input.map((item) => compileConditionNode(item));
  }
  if (import_lodash.default.isPlainObject(input)) {
    if (Object.prototype.hasOwnProperty.call(input, "source")) {
      return compileReactionValueExpr(input);
    }
    const output = {};
    for (const [key, value] of Object.entries(input)) {
      if (key === "path") {
        output[key] = barePathToVariableExpression(value, "condition.path");
        continue;
      }
      if (key === "value") {
        output[key] = compileConditionNode(value);
        continue;
      }
      output[key] = compileConditionNode(value);
    }
    return output;
  }
  return import_lodash.default.cloneDeep(input);
}
function normalizeReactionFilter(filter) {
  if (import_lodash.default.isNil(filter)) {
    return import_lodash.default.cloneDeep(EMPTY_CONDITION);
  }
  if (!import_lodash.default.isPlainObject(filter)) {
    (0, import_errors.throwBadRequest)("flowSurfaces reaction when must be an object");
  }
  const visit = (input) => {
    if (Array.isArray(input)) {
      return input.map((item) => visit(item));
    }
    if (!import_lodash.default.isPlainObject(input)) {
      return import_lodash.default.cloneDeep(input);
    }
    if (Object.prototype.hasOwnProperty.call(input, "source")) {
      return normalizeReactionValueExpr(input);
    }
    const output = {};
    for (const [key, value] of Object.entries(input)) {
      if (key === "path") {
        output[key] = normalizeBarePath(value, "condition.path");
        continue;
      }
      if (key === "value") {
        output[key] = visit(value);
        continue;
      }
      output[key] = visit(value);
    }
    return output;
  };
  return visit(filter);
}
function compileReactionFilter(filter) {
  return compileConditionNode(normalizeReactionFilter(filter));
}
function normalizeReactionPath(path, label) {
  return normalizeBarePath(path, label);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  barePathToVariableExpression,
  compileReactionFilter,
  compileReactionValueExpr,
  normalizeReactionFilter,
  normalizeReactionPath,
  normalizeReactionValueExpr
});
