import { message } from 'antd';
import Button from 'client/Elements/Button';
import { openNotification } from 'client/Elements/Notification';
import { useStores } from 'client/hooks/useStores';
import { Process } from 'core/types/Calculation/Store/process';
import { ElementStatus } from 'types/elements';

const BottomControls = ({ insType, onSelectRow, selectedKey }) => {
  const { calculationStore } = useStores();
  const { ELTStore } = calculationStore.stores;
  const handleSelectRow = () => {
    const { calculationProcess } = calculationStore.stores;
    calculationProcess.addProcess(Process.ELT);
    const selectRes = onSelectRow.call(calculationStore, insType, selectedKey);

    if (selectRes?.message) {
      openNotification({
        type: 'warning',
        message: 'Внимание',
        description: selectRes.message,
      });
    } else {
      ELTStore[insType].setKey(selectedKey);
      message.success({ content: 'Выбранный расчет ЭЛТ применен' });
    }
    calculationProcess.deleteProcess(Process.ELT);
  };

  return (
    <Button
      text="Применить"
      action={handleSelectRow}
      status={
        selectedKey === undefined
          ? ElementStatus.Disabled
          : ElementStatus.Default
      }
    />
  );
};

export default BottomControls;
