import 'package:bloc/bloc.dart';
import 'package:enaklo_pos/data/datasources/file_remote_datasource.dart';
import 'package:enaklo_pos/data/models/response/file_response_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'upload_file_event.dart';
part 'upload_file_state.dart';
part 'upload_file_bloc.freezed.dart';

class UploadFileBloc extends Bloc<UploadFileEvent, UploadFileState> {
  final FileRemoteDataSource _fileRemoteDataSource;
  UploadFileBloc(this._fileRemoteDataSource)
      : super(UploadFileState.initial()) {
    on<_Upload>((event, emit) async {
      emit(_Loading());
      final result = await _fileRemoteDataSource.uploadFile(
        filePath: event.filePath,
        fileType: 'image',
        description: 'Product Image',
      );

      result.fold((l) {
        emit(_Error(l));
      }, (r) {
        emit(_Success(r.data));
      });
    });
  }
}
