import type { Rule } from '../rule.js' import * as Ruleset from '../methods/set.js' import { RuleId } from './id.js' export type Validatable = { isValid?: (props: any) => boolean } export const OptionalArray = (fn: ((value: any) => T) & Validatable, value: any): T[] => value == null ? null : ValidatedArray(fn, value) export const ValidatedArray = (fn: ((value: any) => T) & Validatable, value: any): T[] => Array.from(value || []) .filter((val) => val != null) .map(fn) .filter((value) => { return typeof fn.isValid == 'function' ? fn.isValid(value) : true }) export const FixElementRuleIds = (type: Rule['type'], ids: RuleId[], styles: Rule[]) => { if (ids == null) ids = [] const blankId = Ruleset.findBlankId(styles, type) const styleIdsOfType = Ruleset.filterByType(styles, type).map((s) => s.details.id) const filtered = ids.filter((id) => styleIdsOfType.includes(id)) if (filtered.length) return filtered if (ids.length && !filtered.length) return [blankId] // if (blankId) return [blankId] return [] }