import { isEqual } from 'lodash' ; import { Schema } from '../../../shared/schema/types' ; import { DEFAULT_FIELD_SETTINGS , DISABLED_FIELD_SETTINGS } from './constants' ; import { FieldResultSetting , FieldResultSettingObject , ServerFieldResultSetting , ServerFieldResultSettingObject , } from './types' ; const updateAllFields = ( fields , newValue ) => { return Object . keys ( fields ) . reduce ( ( acc , fieldName ) => ( { ... acc , [ fieldName ] : { ... newValue } } ) , { } ) ; } ; const convertToFieldResultSetting = ( serverFieldResultSetting ) => { const fieldResultSetting = { raw : ! ! serverFieldResultSetting . raw , snippet : ! ! serverFieldResultSetting . snippet , snippetFallback : ! ! ( serverFieldResultSetting . snippet && typeof serverFieldResultSetting . snippet === 'object' && serverFieldResultSetting . snippet . fallback ) , } ; if ( serverFieldResultSetting . raw && typeof serverFieldResultSetting . raw === 'object' && serverFieldResultSetting . raw . size ) { fieldResultSetting . rawSize = serverFieldResultSetting . raw . size ; } if ( serverFieldResultSetting . snippet && typeof serverFieldResultSetting . snippet === 'object' && serverFieldResultSetting . snippet . size ) { fieldResultSetting . snippetSize = serverFieldResultSetting . snippet . size ; } return fieldResultSetting ; } ; export const clearAllFields = ( fields ) => updateAllFields ( fields , { } ) ; export const resetAllFields = ( fields ) => updateAllFields ( fields , DEFAULT_FIELD_SETTINGS ) ; export const convertServerResultFieldsToResultFields = ( serverResultFields , schema ) => { const resultFields = Object . keys ( schema ) . reduce ( ( acc , fieldName ) => ( { ... acc , [ fieldName ] : serverResultFields [ fieldName ] ? convertToFieldResultSetting ( serverResultFields [ fieldName ] ) : DISABLED_FIELD_SETTINGS , } ) , { } ) ; return resultFields ; } ; export const convertToServerFieldResultSetting = ( fieldResultSetting ) => { const serverFieldResultSetting = { } ; if ( fieldResultSetting . raw ) { serverFieldResultSetting . raw = { } ; if ( fieldResultSetting . rawSize ) { serverFieldResultSetting . raw . size = fieldResultSetting . rawSize ; } } if ( fieldResultSetting . snippet ) { serverFieldResultSetting . snippet = { } ; if ( fieldResultSetting . snippetFallback ) { serverFieldResultSetting . snippet . fallback = fieldResultSetting . snippetFallback ; } if ( fieldResultSetting . snippetSize ) { serverFieldResultSetting . snippet . size = fieldResultSetting . snippetSize ; } } return serverFieldResultSetting ; } ; export const splitResultFields = ( resultFields , schema ) => { const textResultFields = { } ; const nonTextResultFields = { } ; const keys = Object . keys ( schema ) ; keys . forEach ( ( fieldName ) => { ( schema [ fieldName ] === 'text' ? textResultFields : nonTextResultFields ) [ fieldName ] = resultFields [ fieldName ] ; } ) ; return { textResultFields , nonTextResultFields } ; } ; export const areFieldsEmpty = ( fields ) => { const anyNonEmptyField = Object . values ( fields ) . find ( ( field ) => { return ( field as FieldResultSetting ) . raw || ( field as FieldResultSetting ) . snippet ; } ) ; return ! anyNonEmptyField ; } ; export const areFieldsAtDefaultSettings = ( fields ) => { const anyNonDefaultSettingsValue = Object . values ( fields ) . find ( ( resultSettings ) => { return ! isEqual ( resultSettings , DEFAULT_FIELD_SETTINGS ) ; } ) ; return ! anyNonDefaultSettingsValue ; } ;