import type { Elements } from '../config/map/values';
import { useStoreValue } from './hooks';
import { useOptions } from '@/stores/calculation/options/hooks';
import { useStatus } from '@/stores/calculation/statuses/hooks';
import { useValidation } from '@/stores/calculation/validation/hooks';
import type { Values } from '@/stores/calculation/values/types';
import { observer } from 'mobx-react-lite';
import type { ComponentType } from 'react';

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

export default function buildOptions<T>(
  Component: ComponentType<T>,
  { elementName, valueName }: BuilderProps
) {
  return observer((props: T) => {
    const [value, setValue] = useStoreValue(valueName);
    const status = useStatus(elementName);
    const { isValid, help } = useValidation(elementName);
    const options = useOptions(elementName);

    return (
      <Component
        help={help}
        isValid={isValid}
        options={options}
        setValue={setValue}
        status={status}
        value={value}
        {...props}
      />
    );
  });
}
