/***********************************************************************************************************
 * Name         : OpportunityTriggerHelper
 * Purpose      : Helper methods for OpportunityTrigger
 **********************************************************************************************************/

public with sharing class OpportunityTriggerHelper {
    public static final Id AWAL_RECTYPE_ID;
    public static final Map<String, Id> CA_RECORD_TYPES_MAP = new Map<String, Id>();
    public static final Map<Id, String> CA_RECORD_TYPES_BY_ID_MAP = new Map<Id, String>();

    static {
        Map<String, Schema.RecordTypeInfo> recordTypeInfosMap = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName();
        for (String developerName : recordTypeInfosMap.keySet()) {
            Id recordTypeId = recordTypeInfosMap.get(developerName).getRecordTypeId();
            if (CA_Constants.OPP_RECORD_TYPES.contains(developerName)) {
                CA_RECORD_TYPES_MAP.put(developerName, recordTypeId);
                CA_RECORD_TYPES_BY_ID_MAP.put(recordTypeId, developerName);
            } else if (developerName == AWAL_Constants.AWAL) {
                AWAL_RECTYPE_ID = recordTypeId;
            }
        }
    }

    /**
     * @description Filter Opportunities by record type
     * @param opps          Trigger.new
     * @param recordTypes   Set of record type Ids
     * @return List<Opportunity> filtered opps
     */
    public static List<Opportunity> filterOpportunities(List<Opportunity> opps, Set<Id> recordTypes) {
        List<Opportunity> filteredOpps = new List<Opportunity>();
        for (Opportunity opp : opps) {
            if (recordTypes.contains(opp.RecordTypeId)) {
                filteredOpps.add(opp);
            }
        }
        return filteredOpps;
    }

    /**
     * @description Remove opportunities from the oldMap that are not in the filteredOpps
     * @param opps          List of filtered opportunities
     * @param oldMapAll     Trigger.oldMap
     * @return List<Opportunity> filtered opps
     */
    public static Map<Id, Opportunity> getFilteredOldMap(List<Opportunity> opps, Map<Id, Opportunity> oldMapAll) {
        Map<Id, Opportunity> oldMap = new Map<Id, Opportunity>();
        for (Opportunity opp : opps) {
            oldMap.put(opp.Id, oldMapAll.get(opp.Id));
        }
        return oldMap;
    }
}