use super::inputs::SelectedCompanyInputs;
use super::model::SelectedCompanySummaryFields;

use crate::derived::runtime_company_annual_finance_policy_action_label;

pub(super) fn fill_selected_company_annual_finance_fields(
    fields: &mut SelectedCompanySummaryFields,
    inputs: &SelectedCompanyInputs<'_>,
) {
    fields.selected_company_annual_finance_policy_action = inputs
        .annual_finance_policy_state
        .as_ref()
        .map(|policy_state| {
            runtime_company_annual_finance_policy_action_label(policy_state.action).to_string()
        });
    fields.selected_company_annual_finance_news_family_candidate = inputs
        .selected_company_id
        .and_then(|company_id| {
            inputs
                .state
                .service_state
                .annual_finance_last_news_family_candidates
                .get(&company_id)
        })
        .cloned();
    fields.selected_company_annual_finance_last_news_selector = inputs
        .selected_company_id
        .and_then(|company_id| {
            inputs
                .state
                .service_state
                .annual_finance_last_news_events
                .iter()
                .rev()
                .find(|news| news.company_id == company_id)
        })
        .map(|news| news.selector_label.clone());
    fields.annual_finance_last_news_event_count = inputs
        .state
        .service_state
        .annual_finance_last_news_events
        .len();
    fields.selected_company_annual_finance_policy_creditor_pressure_bankruptcy_eligible = inputs
        .annual_finance_policy_state
        .as_ref()
        .map(|policy_state| policy_state.creditor_pressure_bankruptcy_eligible);
    fields.selected_company_annual_finance_policy_deep_distress_bankruptcy_fallback_eligible =
        inputs
            .annual_finance_policy_state
            .as_ref()
            .map(|policy_state| policy_state.deep_distress_bankruptcy_fallback_eligible);
    fields.selected_company_annual_finance_policy_bond_issue_eligible = inputs
        .annual_finance_policy_state
        .as_ref()
        .map(|policy_state| policy_state.bond_issue_eligible);
    fields.selected_company_annual_finance_policy_stock_repurchase_eligible = inputs
        .annual_finance_policy_state
        .as_ref()
        .map(|policy_state| policy_state.stock_repurchase_eligible);
    fields.selected_company_annual_finance_policy_stock_issue_eligible = inputs
        .annual_finance_policy_state
        .as_ref()
        .map(|policy_state| policy_state.stock_issue_eligible);
    fields.selected_company_annual_finance_policy_dividend_adjustment_eligible = inputs
        .annual_finance_policy_state
        .as_ref()
        .map(|policy_state| policy_state.dividend_adjustment_eligible);
}
