import { type AnyObject, type AnyValue } from '@squirrel/components/p-select-list/p-select-list.types'; import { isObject } from '@squirrel/utils/object'; export const createInternalItems = (items: unknown[]): AnyObject[] => { let arr = Array.isArray(items) ? items.slice() : []; // The select-list operates with an array of objects internally. // If it's a plain array, we need to remove duplicate values and convert it to an array of objects if (arr.length && !isObject(arr[0])) { arr = arr.filter((item, i, self) => self.indexOf(item) === i).map((item) => ({ value: item, text: item })); } return arr as AnyObject[]; }; export const createInternalValue = ( modelValue: AnyValue | AnyValue[] | AnyObject | AnyObject[], valueIsObject: boolean, multiple: boolean, itemValue: AnyValue, internalItems: AnyObject[] ) => { // Create internalValue let val = modelValue; // If it's an array of objects or an object convert it to a `string[]` or `string` if (valueIsObject && isObject(modelValue)) { if (multiple) { val = (modelValue as AnyObject[]).map((v) => v[itemValue as keyof typeof modelValue]) as AnyValue[]; } else { val = modelValue ? (modelValue as AnyObject)[itemValue as keyof typeof modelValue] : modelValue; } } const arr = Array.isArray(val) ? val : [val]; return arr; };