import { Injectable } from '@angular/core';
import { GenericService } from '@iqser/common-ui';
import { IEntityLog, ISectionGrid } from '@red/domain';
import { firstValueFrom, of } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable({
    providedIn: 'root',
})
export class EntityLogService extends GenericService<unknown> {
    protected readonly _defaultModelPath = '';

    async getEntityLog(dossierId: string, fileId: string) {
        const queryParams = [{ key: 'includeUnprocessed', value: true }];
        const entityLog$ = this._getOne<IEntityLog>([dossierId, fileId], 'entityLog', queryParams);
        const entityLog = await firstValueFrom(entityLog$.pipe(catchError(() => of({} as IEntityLog))));
        entityLog.entityLogEntry.sort((a, b) => a.positions[0].pageNumber - b.positions[0].pageNumber);
        return entityLog;
    }

    getSectionGrid(dossierId: string, fileId: string) {
        return this._getOne<ISectionGrid>([dossierId, fileId], 'sectionGrid');
    }
}
