import type { MergeFunc } from '@assets/filters/common/state-parsers/mergeConfigs';

export const mergeConvertorsConfig: MergeFunc<'convertors'> = (old, updated) => {
   return old.map(convertor => {
      const updatedTypes = updated
         ?.find(u => u.id === convertor.id)
         ?.types ?? [];

      return {
         ...convertor,
         types: convertor.types.map(convertorType => {
            const updatedValues = updatedTypes
               ?.find(u => u.name === convertorType.name)
               ?.values ?? [];

            return {
               ...convertorType,
               values: convertorType.values.map(value => {
                  const updatedCount = updatedValues
                     ?.find(u => u.id === value.id)
                     ?.count ?? 0;

                  return {
                     ...value,
                     count: updatedCount,
                  };
               }),
            };
         }),
      };
   });
};
