/**
 * Namespace utilities for workflow variable resolution.
 *
 * IMPORTANT: The sanitizeNamespace function must stay in sync with:
 * - PHP: plugin/includes/class-sequensy-workflow-executor.php::sanitize_namespace()
 *
 * Any changes here must be mirrored in the PHP implementation to ensure
 * frontend variable paths match backend context keys.
 */

/**
 * Sanitizes a label to be used as a namespace key.
 * Converts to snake_case and removes special characters.
 *
 * Examples:
 * - "Create Post" -> "create_post"
 * - "Send Email" -> "send_email"
 * - "My Action #1" -> "my_action_1"
 *
 * @param label - The label to sanitize
 * @returns Sanitized namespace key
 */
export function sanitizeNamespace(label: string): string {
  return label
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '_')
    .replace(/^_+|_+$/g, '');
}
