import { DcValidation } from '../models/dc-validation.model'
import { DQRule } from '../models/dq-rules.model'
import { getColumnDefault } from './getColumnDefault'

const schemaTypeMap: { [key: string]: any } = {
  numeric: '',
  default: ''
}

/**
 * Schema defines the default values for given types. For example when new row is added.
 * Priority: NOTNULL/READONLY/HIDDEN RULE_VALUE > autocomplete first option > type default
 */
export function getHotDataSchema(
  type: string | undefined,
  cellValidation?: DcValidation,
  dqRules?: DQRule[]
): any {
  // Check for a rule-supplied default (NOTNULL/READONLY/HIDDEN) first
  if (dqRules && cellValidation?.data) {
    const defaultValue = getColumnDefault(cellValidation.data, dqRules, type)
    if (defaultValue !== undefined) {
      return defaultValue
    }
  }

  if (!type) return schemaTypeMap.default

  switch (type) {
    case 'dropdown':
    case 'autocomplete': {
      return cellValidation && cellValidation.source
        ? (cellValidation.source as string[] | number[])[0]
        : []
    }
    default: {
      if (schemaTypeMap.hasOwnProperty(type)) return schemaTypeMap[type]

      return schemaTypeMap.default
    }
  }
}
