import type { Data } from "../../../types";
import { ErrorSources, ErrorType } from "../../../types";
import config from "@/config";

// Define the Props interface for ValidationStepNew
export interface Props<T extends string> {
  initialData: RowData<T>[];
  file?: File;
  onBack?: () => void;
  onNext?: (data: RowData<T>[]) => void;
  isFromScratch?: boolean;
}

// Extended Data type with meta information
export type RowData<T extends string> = Data<T> & {
  __index?: string;
  __template?: string;
  __original?: Record<string, any>;
  __corrected?: Record<string, any>;
  __changes?: Record<string, boolean>;
  upc?: string;
  barcode?: string;
  supplier?: string;
  company?: string;
  item_number?: string;
  [key: string]: any; // Allow any string key for dynamic fields
};

// Template interface
export interface Template {
  id: number;
  company: string;
  product_type: string;
  [key: string]: string | number | boolean | undefined;
}

// Props for the useValidationState hook
export interface ValidationStateProps<T extends string> extends Props<T> {}

// Interface for validation results
export interface ValidationResult {
  error?: boolean;
  message?: string;
  data?: Record<string, any>;
  type?: ErrorType;
  source?: ErrorSources;
}

// Filter state interface
export interface FilterState {
  searchText: string;
  showErrorsOnly: boolean;
  filterField: string | null;
  filterValue: string | null;
}

// UI validation state interface for useUpcValidation
export interface ValidationState {
  validatingCells: Set<string>;  // Using rowIndex-fieldKey as identifier
  itemNumbers: Map<number, string>;  // Using rowIndex as key
  validatingRows: Set<number>;  // Rows currently being validated
  activeValidations: Set<string>;  // Active validations
}

// InfoWithSource interface for validation errors
export interface InfoWithSource {
  message: string;
  level: 'info' | 'warning' | 'error';
  source: ErrorSources;
  type: ErrorType;
}

// Template state interface
export interface TemplateState {
  selectedTemplateId: string | null;
  showSaveTemplateDialog: boolean;
  newTemplateName: string;
  newTemplateType: string;
}

// Add config at the top of the file
// Import the config or access it through window
declare global {
  interface Window {
    config?: {
      apiUrl: string;
    };
  }
}

// Use a helper to get API URL consistently
export const getApiUrl = () => config.apiUrl;

// Shared utility function for checking empty values
export const isEmpty = (value: any): boolean => 
  value === undefined || 
  value === null || 
  value === '' || 
  (Array.isArray(value) && value.length === 0) ||
  (typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length === 0); 