import {
  type NodeProps,
  Position,
  type Node, useNodeConnections
} from '@xyflow/react';
import {useEffect} from "react";
import type {EditorWarning} from "../components/EditorWarnings.tsx";
import { Toolbar } from '../components/NodeComponents';
import styles from '../../VisProg.module.css';
import {SingleConnectionHandle} from "../components/RuleBasedHandle.tsx";
import {allowOnlyConnectionsFromType} from "../HandleRules.ts";
import useFlowStore from "../VisProgStores.tsx";



/**
 * The typing of this node's data
 */
export type EndNodeData = {
  label: string;
  droppable: boolean;
  hasReduce: boolean;
};

export type EndNode = Node<EndNodeData>

/**
 * Default function to render an end node given its properties
 * @param props the node's properties
 * @returns React.JSX.Element
 */
export default function EndNode(props: NodeProps<EndNode>) {
  const {registerWarning, unregisterWarning} = useFlowStore.getState();
  const connections = useNodeConnections({
    id: props.id,
    handleId: 'target'
  })

  useEffect(() => {
    const noConnectionWarning : EditorWarning = {
      scope: {
        id: props.id,
        handleId: 'target'
      },
      type: 'MISSING_INPUT',
      severity: "ERROR",
      description: "the endNode does not have an incoming connection from a phaseNode"
    }

    if (connections.length === 0) { registerWarning(noConnectionWarning); }
    else { unregisterWarning(props.id, `${noConnectionWarning.type}:target`); }
  }, [connections.length, props.id, registerWarning, unregisterWarning]);

  return (
    <>
      <Toolbar nodeId={props.id} allowDelete={false}/>
      <div className={`${styles.defaultNode} ${styles.nodeEnd}`}>
        <div className={"flex-row gap-sm"}>
        End
        </div>
        <SingleConnectionHandle type="target" position={Position.Left} id="target" rules={[
          allowOnlyConnectionsFromType(["phase"])
        ]} title="Connect to a phaseNode"/>
      </div>
    </>
  );
}

/**
 * Functionality for reducing this node into its more compact json program
 * @param node the node to reduce
 * @param _nodes all nodes present
 * @returns Dictionary, {id: node.id}
 */
export function EndReduce(node: Node, _nodes: Node[]) {
  // Replace this for nodes functionality
  return {
    id: node.id
  }
}

export const EndTooltip = `
  The end node signifies the endpoint of your program; 
  the output of the final phase of your program should connect to the end node`;

/**
 * This function is called whenever a connection is made with this node type as the target
 * @param _thisNode the node of this node type which function is called
 * @param _sourceNodeId the source of the received connection
 */
export function EndConnectionTarget(_thisNode: Node, _sourceNodeId: string) {
  // no additional connection logic exists yet
}

/**
 * This function is called whenever a connection is made with this node type as the source
 * @param _thisNode the node of this node type which function is called
 * @param _targetNodeId the target of the created connection
 */
export function EndConnectionSource(_thisNode: Node, _targetNodeId: string) {
  // no additional connection logic exists yet
}

/**
 * This function is called whenever a connection is disconnected with this node type as the target
 * @param _thisNode the node of this node type which function is called
 * @param _sourceNodeId the source of the disconnected connection
 */
export function EndDisconnectionTarget(_thisNode: Node, _sourceNodeId: string) {
  // no additional connection logic exists yet
}

/**
 * This function is called whenever a connection is disconnected with this node type as the source
 * @param _thisNode the node of this node type which function is called
 * @param _targetNodeId the target of the diconnected connection
 */
export function EndDisconnectionSource(_thisNode: Node, _targetNodeId: string) {
  // no additional connection logic exists yet
}