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

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

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

    return (
      <Component
        href={status === 'Disabled' ? undefined : value || undefined}
        disabled={!value || status === 'Disabled'}
        loading={status === 'Loading'}
        {...props}
      />
    );
  });
}
