import { IsoObjectUtils } from "@upload-io/upload-shared-iso/common/IsoObjectUtils";
import {
  StorageStatisticsSegmentDto,
  StorageStatisticsSegmentUnboxedDto
} from "@upload-io/upload-shared-iso/statistics/storage/dtos/StorageStatisticsSegmentDto";
import {
  UploadStatisticsDto,
  UploadStatisticsUnboxedDto
} from "@upload-io/upload-shared-iso/statistics/storage/dtos/UploadStatisticsDto";

export interface StorageStatisticsDto {
  caches: {
    [cacheId: string]: StorageStatisticsSegmentDto | undefined;
  };
  uploads: UploadStatisticsDto;
}

export interface StorageStatisticsUnboxedDto {
  caches: {
    [cacheId: string]: StorageStatisticsSegmentUnboxedDto | null;
  };
  uploads: UploadStatisticsUnboxedDto;
}

export namespace StorageStatisticsDto {
  export const empty: StorageStatisticsDto = {
    caches: {},
    uploads: UploadStatisticsDto.empty
  };

  export function combine(left: StorageStatisticsDto, right: StorageStatisticsDto): StorageStatisticsDto {
    return {
      uploads: UploadStatisticsDto.combine(left.uploads, right.uploads),
      caches: IsoObjectUtils.fullOuterJoin(left.caches, right.caches, StorageStatisticsSegmentDto.combine)
    };
  }
}
