HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vm8 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: afleverb (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/drakkar_site/wp-content/themes/blocksy/static/js/options/helpers/get-value-from-input.js
import { isString, isObject, isNumber, isBoolean } from './primitive-types'

export const getFirstLevelOptions = (options, hasInnerOptions = true) => {
	const { __CT_KEYS_ORDER__, ...rest } = options

	return Object.keys(rest).reduce((currentOptions, currentOptionId) => {
		if (!options[currentOptionId].type) {
			return {
				...currentOptions,
				...getFirstLevelOptions(
					options[currentOptionId],
					hasInnerOptions
				),
			}
		}

		if (options[currentOptionId].options) {
			return {
				...currentOptions,
				...getFirstLevelOptions(
					options[currentOptionId].options,
					hasInnerOptions
				),
			}
		}

		if (options[currentOptionId]['inner-options'] && hasInnerOptions) {
			return {
				...currentOptions,
				[currentOptionId]: options[currentOptionId],
				...getFirstLevelOptions(
					options[currentOptionId]['inner-options'],
					hasInnerOptions
				),
			}
		}

		return {
			...currentOptions,
			[currentOptionId]: options[currentOptionId],
		}
	}, {})
}

export const flattenOptions = (options) =>
	Object.keys(options).reduce(
		(result, currentId) => ({
			...result,

			...(options[currentId].type
				? { [currentId]: options[currentId] }
				: currentId === '__CT_KEYS_ORDER__'
				? { [currentId]: options[currentId] }
				: flattenOptions(options[currentId])),
		}),
		{}
	)

export const getValueFromInput = (
	options,
	values,
	valueGetter = null,
	hasInnerOptions = true
) => {
	let firstLevelOptions = getFirstLevelOptions(options, hasInnerOptions)

	return {
		...values,
		...Object.keys(firstLevelOptions).reduce(
			(currentValues, currentOptionId) => {
				let actualValue = null

				if (Object.keys(values).indexOf(currentOptionId) > -1) {
					if (
						isString(values[currentOptionId]) ||
						isNumber(values[currentOptionId]) ||
						isBoolean(values[currentOptionId])
					) {
						actualValue = values[currentOptionId]
					}

					if (
						isObject(values[currentOptionId]) &&
						!Array.isArray(values[currentOptionId])
					) {
						// Dont touch responsive values and dont spread all keys
						// together.
						actualValue = {
							// ...(firstLevelOptions[currentOptionId].value || {}),
							...values[currentOptionId],
						}
					}

					if (Array.isArray(values[currentOptionId])) {
						actualValue = values[currentOptionId]
							? values[currentOptionId]
							: [
									...(firstLevelOptions[currentOptionId]
										.value || []),
									// ...values[currentOptionId],
							  ]
					}
				} else if (valueGetter) {
					return {
						...currentValues,
						...valueGetter(
							currentOptionId,
							firstLevelOptions[currentOptionId]
						),
					}
				} else {
					if (
						Object.keys(firstLevelOptions[currentOptionId]).indexOf(
							'value'
						) > -1
					) {
						actualValue = firstLevelOptions[currentOptionId].value
					} else {
						actualValue = ''
					}
				}

				return {
					...currentValues,
					[currentOptionId]: actualValue,
				}
			},
			{}
		),
	}
}