import { Injectable } from '@angular/core';
import { File, IFile } from '@red/domain';
import { EntitiesMapService } from '@iqser/common-ui';
import { NonFunctionKeys } from '@iqser/common-ui/lib/utils';

@Injectable({ providedIn: 'root' })
export class FilesMapService extends EntitiesMapService<IFile, File> {
    replaceFiles<T extends NonFunctionKeys<File>, T2 extends File[T]>(files: File[], property: T, generateValue: (value: T2) => T2) {
        const newFiles = files.map(
            file =>
                new File(
                    {
                        ...file,
                        [property]: generateValue(file[property] as T2),
                        lastUpdated: new Date().toISOString(),
                    },
                    file.reviewerName,
                ),
        );
        this.replace(newFiles[0].dossierId, newFiles);
    }
}
