async function generate(context) {
  return `/**
 * Utility functions for form field handling
 */

/**
 * Check if a field should be hidden because it's auto-generated
 */
export function isAutoGeneratedField(attrName: string, attrDef: any): boolean {
  const typeStr = typeof attrDef === 'string' ? attrDef : attrDef?.type || '';

  // Hide UUID id field
  if (attrName === 'id' && typeStr.includes('UUID')) return true;

  // Hide auto-generated DateTime fields
  const autoDateFields = ['createdAt', 'updatedAt', 'joinedAt', 'publishedAt'];
  if (autoDateFields.includes(attrName) && typeStr.includes('DateTime')) return true;

  return false;
}

/**
 * Check if a field is required
 */
export function isFieldRequired(attrDef: any): boolean {
  if (!attrDef) return false;
  if (typeof attrDef === 'string') {
    return attrDef.toLowerCase().includes('required');
  }
  return attrDef.required === true;
}

/**
 * Get default value for a field based on its type
 */
export function getFieldDefaultValue(attrDef: any): any {
  if (!attrDef) return '';

  const typeStr = typeof attrDef === 'string' ? attrDef : attrDef?.type || '';

  // Check if schema has a default value
  if (attrDef && typeof attrDef === 'object' && attrDef.default !== undefined) {
    const defaultVal = attrDef.default;
    // Parse string defaults to proper types
    if (typeStr.toLowerCase().includes('bool')) {
      return defaultVal === 'true' || defaultVal === true;
    } else if (
      typeStr.toLowerCase().includes('int') ||
      typeStr.toLowerCase().includes('number')
    ) {
      return typeof defaultVal === 'number' ? defaultVal : parseInt(defaultVal) || 0;
    } else if (typeStr.toLowerCase().includes('string')) {
      // Remove surrounding quotes if present
      return typeof defaultVal === 'string'
        ? defaultVal.replace(/^"|"$/g, '')
        : defaultVal;
    } else {
      return defaultVal;
    }
  }

  // Otherwise use type-based defaults
  if (typeStr.toLowerCase().includes('bool')) {
    return false;
  } else if (
    typeStr.toLowerCase().includes('int') ||
    typeStr.toLowerCase().includes('number')
  ) {
    return 0;
  } else {
    return '';
  }
}

/**
 * Initialize form data from schema attributes
 */
export function initializeFormData(
  attributes: Record<string, any>,
  options: { includeAutoGenerated?: boolean } = {}
): Record<string, any> {
  const data: Record<string, any> = {};

  Object.entries(attributes).forEach(([attrName, attrDef]) => {
    // Skip auto-generated fields unless explicitly included
    if (!options.includeAutoGenerated && isAutoGeneratedField(attrName, attrDef)) {
      return;
    }

    data[attrName] = getFieldDefaultValue(attrDef);
  });

  return data;
}
`;
}
export {
  generate
};
