import { toDateInputValue } from "@open-mercato/shared/lib/date/format";
function buildRulePayload(formValues, tenantId, organizationId, createdBy) {
  return {
    ruleId: formValues.ruleId,
    ruleName: formValues.ruleName,
    description: formValues.description || null,
    ruleType: formValues.ruleType,
    ruleCategory: formValues.ruleCategory || null,
    entityType: formValues.entityType,
    eventType: formValues.eventType || null,
    conditionExpression: formValues.conditionExpression,
    successActions: formValues.successActions || null,
    failureActions: formValues.failureActions || null,
    enabled: formValues.enabled,
    priority: formValues.priority,
    version: formValues.version,
    effectiveFrom: formValues.effectiveFrom || null,
    effectiveTo: formValues.effectiveTo || null,
    tenantId,
    organizationId,
    createdBy: createdBy || null
  };
}
function parseRuleToFormValues(rule) {
  let conditionExpression = rule.conditionExpression;
  if (conditionExpression && !conditionExpression.operator?.match(/^(AND|OR)$/)) {
    conditionExpression = {
      operator: "AND",
      rules: [conditionExpression]
    };
  }
  return {
    ruleId: rule.ruleId,
    ruleName: rule.ruleName,
    description: rule.description,
    ruleType: rule.ruleType,
    ruleCategory: rule.ruleCategory,
    entityType: rule.entityType,
    eventType: rule.eventType,
    conditionExpression,
    successActions: rule.successActions,
    failureActions: rule.failureActions,
    enabled: rule.enabled,
    priority: rule.priority,
    version: rule.version,
    effectiveFrom: toDateInputValue(rule.effectiveFrom),
    effectiveTo: toDateInputValue(rule.effectiveTo)
  };
}
function generateRuleId(ruleName) {
  return ruleName.toUpperCase().replace(/[^A-Z0-9]+/g, "_").replace(/(?:^_+|_+$)/g, "").substring(0, 50);
}
function getEntityTypeSuggestions() {
  return [
    "WorkOrder",
    "Order",
    "Invoice",
    "Customer",
    "Product",
    "User",
    "Task",
    "Ticket",
    "Deal",
    "Contact"
  ];
}
function getEventTypeSuggestions(entityType) {
  return [
    "beforeCreate",
    "afterCreate",
    "beforeUpdate",
    "afterUpdate",
    "beforeDelete",
    "afterDelete",
    "beforeSave",
    "afterSave",
    "onStatusChange",
    "onAssign"
  ];
}
function formatDate(date) {
  if (!date) return "-";
  const d = typeof date === "string" ? new Date(date) : date;
  if (isNaN(d.getTime())) return "-";
  return d.toLocaleDateString();
}
export {
  buildRulePayload,
  formatDate,
  generateRuleId,
  getEntityTypeSuggestions,
  getEventTypeSuggestions,
  parseRuleToFormValues
};
//# sourceMappingURL=formHelpers.js.map
