import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { OverwriteFilesDialogComponent } from '../dialogs/overwrite-files-dialog/overwrite-files-dialog.component';
import { firstValueFrom } from 'rxjs';
import { OverwriteFileOption } from '@red/domain';

const dialogConfig = {
    width: '662px',
    maxWidth: '90vw',
    autoFocus: false,
};

@Injectable()
export class UploadDownloadDialogService {
    constructor(private readonly _dialog: MatDialog) {}

    openOverwriteFileDialog(
        filename: string,
    ): Promise<{ option?: OverwriteFileOption; rememberChoice?: boolean; applyToAllFiles?: boolean; cancel?: boolean }> {
        const ref = this._dialog.open(OverwriteFilesDialogComponent, {
            ...dialogConfig,
            data: filename,
        });

        return firstValueFrom(ref.afterClosed()).then(res => res || { cancel: true });
    }
}
