import { Nullable, isNotNullish, isNullish } from 'vest-utils'; import { TIsolate } from './Isolate'; import { IsolateStatus } from './IsolateStatus'; export class IsolateInspector { static at(isolate: Nullable, at: number): Nullable { if (isNullish(isolate)) { return null; } return isolate.children?.[at] ?? null; } static cursor(isolate: Nullable): number { if (isNullish(isolate)) { return 0; } return isolate.children?.length ?? 0; } static canReorder(isolate: Nullable): boolean { if (isNullish(isolate)) { return false; } return IsolateInspector.allowsReorder(isolate.parent); } static allowsReorder>( isolate: Nullable, ): boolean { return isolate?.allowReorder === true; } static usesKey(isolate: Nullable): boolean { if (isNullish(isolate)) { return false; } return isNotNullish(isolate.key); } static getChildByKey( isolate: Nullable, key: string, ): Nullable { if (isNullish(isolate)) { return null; } return isolate.keys?.[key] ?? null; } static getStatus(isolate: Nullable): IsolateStatus { if (isNullish(isolate)) { return IsolateStatus.INITIAL; } return isolate.status ?? IsolateStatus.INITIAL; } static statusEquals( isolate: Nullable, status: IsolateStatus, ): boolean { return IsolateInspector.getStatus(isolate) === status; } static isPending(isolate: Nullable): boolean { return IsolateInspector.statusEquals(isolate, IsolateStatus.PENDING); } static isHasPending(isolate: Nullable): boolean { return IsolateInspector.statusEquals(isolate, IsolateStatus.HAS_PENDING); } static hasPending(isolate: Nullable): boolean { return ( IsolateInspector.isPending(isolate) || IsolateInspector.isHasPending(isolate) ); } static hasActiveChildren(isolate: Nullable): boolean { if (isNullish(isolate) || isNullish(isolate.children)) { return false; } // Check if ANY immediate child is currently PENDING or HAS_PENDING return isolate.children.some(child => IsolateInspector.hasPending(child)); } static getParent(isolate: Nullable): Nullable { return isolate?.parent ?? null; } }