parseCompanyFacts method

CompanyFinancials parseCompanyFacts({
  1. required String ticker,
  2. required int cik,
  3. required Map<String, dynamic> facts,
  4. FetchOptions options = FetchOptions.defaultOptions,
})

Parse company facts and extract all metrics for specified years.

Implementation

CompanyFinancials parseCompanyFacts({
  required String ticker,
  required int cik,
  required Map<String, dynamic> facts,
  FetchOptions options = FetchOptions.defaultOptions,
}) {
  final usGaap = facts['facts']?['us-gaap'] as Map<String, dynamic>?;
  final companyName = facts['entityName'] as String?;

  if (usGaap == null) {
    return CompanyFinancials(
      ticker: ticker,
      cik: cik,
      companyName: companyName,
      metrics: {},
      fetchedAt: DateTime.now(),
    );
  }

  final metricsToFetch =
      options.metricsToFetch ?? FinancialMetric.values.toSet();
  final metrics = <FinancialMetric, MetricHistory>{};

  for (final metric in metricsToFetch) {
    final history = _extractMetricHistory(
      usGaap: usGaap,
      metric: metric,
      years: options.years,
      includeQuarterly: options.includeQuarterly,
    );

    if (history.values.isNotEmpty) {
      metrics[metric] = history;
    }
  }

  // Build statements map from metrics
  final statements = _buildStatements(metrics);

  return CompanyFinancials(
    ticker: ticker,
    cik: cik,
    companyName: companyName,
    metrics: metrics,
    statements: statements,
    fetchedAt: DateTime.now(),
  );
}