import { trackSplit, get } from 'ripple';

export function createSplitProps<Target extends Record<string, any>>() {
  return function <Props extends Target & Record<string, any>>(
    props: Props,
    keys: (keyof Target)[],
  ): [Target, Omit<Props, keyof Target>] {
    const vals = trackSplit(props, keys as any) as any[];
    const rest = vals[vals.length - 1];
    const matched: any = {};
    for (let i = 0; i < keys.length; i++) {
      const val = vals[i];
      if (get(val) !== undefined) {
        Object.defineProperty(matched, keys[i] as string, {
          get: () => get(val),
          enumerable: true,
          configurable: true,
        });
      }
    }
    return [matched, rest];
  };
}
