import 'package:dio/dio.dart';
import 'package:niogu_ecommerce_v1/core/errors/exceptions.dart';
import 'package:niogu_ecommerce_v1/core/utils/log_message.dart';
import 'package:niogu_ecommerce_v1/features/cart/data/datasource/cart_remote_datasource_impl.dart';
import 'package:niogu_ecommerce_v1/features/cart/domain/repositories/i_cart_repository.dart';
import 'package:niogu_ecommerce_v1/features/home/domain/entities/home.dart';

class CartRepositoryImpl implements ICartRepository {
  final ICartRemoteDatasource _datasource;

  CartRepositoryImpl(this._datasource);

  @override
  Future<List<ProductItem>> fetchProductBestSellers() async {
    try {
      final response = await _datasource.fetchProductBestSellers();

      final data = response.data;

      if (response.status != 'success' || data == null) return [];

      return data.map((product) => product.toProductItem()).toList();
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);

      throw ServerException("Terjadi kesalahan koneksi");
    }
  }
}
