/**
 * Parses a query parameter value based on its expected type.
 * Throws on invalid formats.
 */
export function parseValue(value, type) {
  if (value === null || value === undefined || value === '') return null;

  switch (type) {
    case 'number': {
      const num = parseFloat(value);
      if (Number.isNaN(num)) throw new Error(`Invalid number format: "${value}"`);
      return num;
    }
    case 'integer': {
      const int = parseInt(value, 10);
      if (Number.isNaN(int)) throw new Error(`Invalid integer format: "${value}"`);
      return int;
    }
    case 'boolean':
      if (String(value).toLowerCase() === 'true') return true;
      if (String(value).toLowerCase() === 'false') return false;
      throw new Error(`Invalid boolean format: "${value}"`);
    case 'date':
      return String(value);
    case 'string':
    default:
      return String(value);
  }
}
