import { observer } from 'mobx-react-lite';
import type { ComponentType } from 'react';
import { useStatus } from 'stores/calculation/statuses/hooks';
import type { ComputedValues } from 'stores/calculation/values/computed';
import { useComputedValue } from 'stores/calculation/values/hooks';
import type { Elements } from '../config/map/computed';

type BuilderProps = {
  elementName: Elements;
  valueName: ComputedValues;
};

export default function buildComputedValue<T>(
  Component: ComponentType<T>,
  { elementName, valueName: computedValueName }: BuilderProps
) {
  return observer((props: T) => {
    const computedValue = useComputedValue(computedValueName);
    const status = useStatus(elementName);

    return <Component value={computedValue} status={status} {...props} />;
  });
}
