/**
 * Resolves a property value for a specific source index
 * @param value - Single string value or array of values
 * @param sourceIndex - Index of the current source
 * @param totalSources - Total number of sources
 * @param propertyName - Name of the property being resolved (for error messages)
 * @returns The resolved string value for the source
 * @internal
 */
export function resolvePropertyForSource(
  value: unknown,
  sourceIndex: number,
  totalSources: number,
  propertyName: string,
) {
  if (!value) return undefined
  if (!Array.isArray(value)) return value

  if (value.length === 1) return value.at(0) as unknown
  if (value.length !== totalSources) {
    throw new Error(
      `${propertyName} array length (${value.length}) must match sources length (${totalSources}). ` +
        `Either provide a single ${propertyName.toLowerCase()} for all sources, or one ${propertyName.toLowerCase()} per source.`,
    )
  }
  return value[sourceIndex]! as unknown
}

/**
 * Resolves the column value for a specific source index
 * @param column - Single column string or array of columns
 * @param sourceIndex - Index of the current source
 * @param totalSources - Total number of sources
 * @returns The resolved column string for the source
 * @internal
 */
export function resolveColumnForSource(
  column: string | string[] | undefined,
  sourceIndex: number,
  totalSources: number,
) {
  return resolvePropertyForSource(column, sourceIndex, totalSources, 'column')
}
