import { DQRule } from '../models/dq-rules.model'

/**
 * Returns the NOTNULL default value for a column from DQ rules.
 * Converts to number for numeric columns based on colType parameter.
 *
 * @param colName column name to look up
 * @param dqRules array of DQ rules
 * @param colType column type (e.g., 'numeric', 'text')
 * @returns default value (string or number) if NOTNULL rule exists with non-empty value, otherwise undefined
 */
export function getNotNullDefault(
  colName: string,
  dqRules: DQRule[],
  colType?: string
): string | number | undefined {
  const notNullRule = dqRules.find(
    (rule: DQRule) => rule.BASE_COL === colName && rule.RULE_TYPE === 'NOTNULL'
  )

  if (!notNullRule?.RULE_VALUE || notNullRule.RULE_VALUE.trim().length === 0) {
    return undefined
  }

  if (colType === 'numeric' && !isNaN(Number(notNullRule.RULE_VALUE))) {
    return Number(notNullRule.RULE_VALUE)
  }

  return notNullRule.RULE_VALUE
}
