import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("workflows");
function generateNameFromId(edgeId) {
  return edgeId.split("_to_").map((part) => part.split("_").map(
    (word) => word.charAt(0).toUpperCase() + word.slice(1)
  ).join(" ")).join(" \u2192 ");
}
function normalizeCondition(raw) {
  if (typeof raw === "string") {
    return { ruleId: raw, required: true };
  }
  return raw;
}
function normalizeConditions(raw) {
  if (!Array.isArray(raw)) return [];
  return raw.map(normalizeCondition);
}
function edgeToFormValues(edge) {
  const edgeData = edge.data;
  let transitionName = "";
  if (edgeData?.transitionName && edgeData.transitionName !== "") {
    transitionName = edgeData.transitionName;
  } else if (edgeData?.label && edgeData.label !== "" && edgeData.label !== void 0) {
    transitionName = edgeData.label;
  } else {
    transitionName = generateNameFromId(edge.id);
  }
  return {
    transitionName,
    trigger: edgeData?.trigger || "auto",
    priority: (edgeData?.priority || 100).toString(),
    continueOnActivityFailure: edgeData?.continueOnActivityFailure !== void 0 ? edgeData.continueOnActivityFailure : false,
    preConditions: normalizeConditions(edgeData?.preConditions || []),
    postConditions: normalizeConditions(edgeData?.postConditions || []),
    activities: edgeData?.activities || [],
    advancedConfig: ""
    // Advanced config is empty initially (can be populated from edgeData if needed)
  };
}
function formValuesToEdgeUpdates(values, edge) {
  const updates = {
    transitionName: values.transitionName,
    label: values.transitionName,
    // Keep label for backward compatibility
    trigger: values.trigger,
    priority: parseInt(values.priority) || 100,
    continueOnActivityFailure: values.continueOnActivityFailure,
    preConditions: values.preConditions.length > 0 ? values.preConditions : void 0,
    postConditions: values.postConditions.length > 0 ? values.postConditions : void 0,
    activities: values.activities.length > 0 ? values.activities : void 0
  };
  if (values.advancedConfig && values.advancedConfig.trim()) {
    try {
      const parsed = JSON.parse(values.advancedConfig);
      Object.assign(updates, parsed);
    } catch (error) {
      logger.error("Invalid JSON in Advanced Configuration", { err: error });
      throw new Error("Invalid JSON in Advanced Configuration. Please check your syntax.");
    }
  }
  return updates;
}
function getExcludedPreConditionRuleIds(values) {
  return values.preConditions.map((c) => {
    const normalized = normalizeCondition(c);
    return normalized.ruleId;
  });
}
function getExcludedPostConditionRuleIds(values) {
  return values.postConditions.map((c) => {
    const normalized = normalizeCondition(c);
    return normalized.ruleId;
  });
}
export {
  edgeToFormValues,
  formValuesToEdgeUpdates,
  getExcludedPostConditionRuleIds,
  getExcludedPreConditionRuleIds,
  normalizeCondition,
  normalizeConditions
};
//# sourceMappingURL=edgeFormTransforms.js.map
