import 'package:money2/money2.dart';

import 'package:pshared/models/payment/currency_pair.dart';
import 'package:pshared/models/payment/fx/intent.dart';
import 'package:pshared/models/payment/fx/side.dart';


class FxIntentHelper {
  static FxIntent? buildSellBaseBuyQuote({
    required String baseCurrency,
    required String quoteCurrency,
    bool enabled = true,
  }) {
    if (!enabled) return null;
    final base = baseCurrency.trim();
    final quote = quoteCurrency.trim();
    if (base.isEmpty || quote.isEmpty) return null;
    if (base.toUpperCase() == quote.toUpperCase()) return null;
    return FxIntent(
      pair: CurrencyPair(base: base, quote: quote),
      side: FxSide.sellBaseBuyQuote,
    );
  }

  static String resolveSettlementCurrency({
    required Money amount,
    FxIntent? fx,
  }) {
    final pair = fx?.pair;
    if (pair != null) {
      switch (fx?.side ?? FxSide.unspecified) {
        case FxSide.buyBaseSellQuote:
          if (pair.base.isNotEmpty) return pair.base;
          break;
        case FxSide.sellBaseBuyQuote:
          if (pair.quote.isNotEmpty) return pair.quote;
          break;
        case FxSide.unspecified:
          break;
      }
      if (amount.currency.isoCode == pair.base && pair.quote.isNotEmpty) {
        return pair.quote;
      }
      if (amount.currency.isoCode == pair.quote && pair.base.isNotEmpty) {
        return pair.base;
      }
      if (pair.quote.isNotEmpty) return pair.quote;
      if (pair.base.isNotEmpty) return pair.base;
    }
    return amount.currency.isoCode;
  }
}
