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

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

export default function buildReadonly<T>(
  Component: ComponentType<T>,
  { elementName, valueName }: BuilderProps
) {
  return observer((props: T) => {
    const [value] = useValue(valueName);
    const status = useStatus(elementName);

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