export function GetCombinedClassesArray(childClasses: Array<string>, externalErrorClasses?: string | null){
    const classes = externalErrorClasses?.trim();
	if (classes == null || classes.length <= 0) return childClasses;
    return childClasses + ' ' + classes.trim().replace(/\s\s+/g, ' ').split(' ').join(' ');
}

// export function GetCombinedClassesstring(childClasses: string | null | undefined, externalErrorClasses?: string | null){
//     const classes = externalErrorClasses?.trim() ?? '';
// 	if (classes == null || classes.length <= 0) return childClasses;
//     return childClasses + ' ' + classes.trim().replace(/\s\s+/g, ' ');
// }

export function GetElementId(prefix: string, itemId: string){
	return prefix + '_' + itemId;
}

export function TruncateString(str: string, length: number) {
	if (str.length <= length) {
		return str
	}
	return str.slice(0, length) + '...'
}

export function SetFlag(flagItem: number, flag: number): number{
  	return flagItem | flag;
} 

export function RemoveFlag(flagItem: number, flag: number): number{
	return flagItem & ~flag;
} 

export function IsFlagSet(actual: number, expected: number): boolean{
	const flag = actual & expected;
	return flag === expected;
};

export function DeepEqual(object1: object | null | undefined, object2: object | null | undefined) {
	if(object1 == null || object2 == null) return false;
	
	const keys1 = Object.keys(object1);
	const keys2 = Object.keys(object2);

	if (keys1.length !== keys2.length) {
	  return false;
	}

	for (const key of keys1) {
	  const val1 = (object1 as keyof typeof object1)[key];
	  const val2 = (object2 as keyof typeof object2)[key];
	  const areObjects = isObject(val1) && isObject(val2);
	  if (
		areObjects && !DeepEqual(val1, val2) ||
		!areObjects && val1 !== val2
	  ) {
		return false;
	  }
	}
  
	return true;
  }
  
  function isObject(object: object) {
	return object != null && typeof object === 'object';
  }

export function GetDate(dateString: string | null | undefined) : Date | null{
	if (dateString == null || dateString == '' || dateString.length == 0){
		return null;
	}
	else{
		// console.log('date:')
		// console.log(new Date(dateString))
		return new Date(dateString);
	}
}

export function DateToString(date: Date | undefined | null): string {
	if (date == null){
		return '';
	}
	else{
		// const d = new Date(date);
		// const year = new Date(date).getFullYear();
		// const month = new Date(date).getMonth();
		// const day = new Date(date).getDay();
		const year = date.getFullYear();
		const month = date.getMonth();
		const day = date.getDate();
		return `${year}-${(month + 1).toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
		// return `${day.toString().padStart(2, '0')}-${(month + 1).toString().padStart(2, '0')}-${year}`;
	}
}

export function GetNumericValue(percentage: number | null | undefined, valueMax: number) : number | undefined{
	// if (!(percentage != undefined && percentage != null && percentage > 0)) return undefined;
	if (!IsNumericValueDefined(percentage)) return undefined;
	else return percentage! / (100 / valueMax);
}

export function GetPercentage(value: number | undefined | null, valueMax: number) : number{
	// return (value != undefined && value != null && value > 0 ? value! : valueMax) / valueMax * 100;
	return (IsNumericValueDefined(value)
		? value! > valueMax
			? valueMax
			: value!
		: valueMax) / valueMax * 100;
}

function IsNumericValueDefined(percentage: number | null | undefined){
	return percentage != undefined && percentage != null && percentage > 0;
}