/***********************************************************************************************************
 * Name         : AccountTriggerHelper
 * Purpose      : Helper methods for AccountTrigger
 **********************************************************************************************************/
public with sharing class AccountTriggerHelper {
    public static final Id CA_RECTYPE_ID;

    static {
        Map<String, Schema.RecordTypeInfo> recordTypeInfosMap = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName();
        for (String developerName : recordTypeInfosMap.keySet()) {
            Id recordTypeId = recordTypeInfosMap.get(developerName).getRecordTypeId();
            if (CA_Constants.RECORD_TYPE_CA == developerName) {
                CA_RECTYPE_ID = recordTypeId;
            }
        }
    }

    /**
     * @description Sets the RecordType for Accounts converted from CA Leads.
     * @param newList List of Account records to process
     */
    public static void setRecordType(List<Account> accounts) {
        Id caRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName()
            .get(CA_Constants.RECORD_TYPE_CA)
            .getRecordTypeId();

        for (Account account : accounts) {
            if (account.ConvertedFromLeadRecordType__c == LeadTriggerHelper.CA_RECTYPE_ID) {
                account.RecordTypeId = AccountTriggerHelper.CA_RECTYPE_ID;
                account.CA_Account_Name__c = account.Name;
            }
        }
    }

    /**
     * @description Filter Accounts by record type
     * @param accounts          Trigger.new
     * @param recordTypes   Set of record type Ids
     * @return List<Account> filtered accounts
     */
    public static List<Account> filterAccounts(List<Account> accounts, Set<Id> recordTypes) {
        List<Account> filteredAccounts = new List<Account>();
        for (Account acc : accounts) {
            if (recordTypes.contains(acc.RecordTypeId)) {
                filteredAccounts.add(acc);
            }
        }
        return filteredAccounts;
    }

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