import { RectNode, RectNodeModel } from '@logicflow/core';
import { START_ID } from '../constant';

class EndNodeModel extends RectNodeModel {
  getDefaultAnchor() {
    return super.getDefaultAnchor().map((anchor) => Object.assign(anchor, { type: 'incoming' }));
  }

  getConnectedSourceRules() {
    const rules = super.getConnectedSourceRules();
    rules.push({
      message: '结束节点不允许输出',
      validate: () => false,
    });
    return rules;
  }

  getConnectedTargetRules() {
    const rules = super.getConnectedTargetRules();
    rules.push({
      message: `当前节点不是输出锚点`,
      validate: (source, target, sourceAnchor, targetAnchor) => {
        return !sourceAnchor?.type || sourceAnchor?.type === 'outgoing';
      }
    })

    rules.push({
      message: '禁止直接连接开始节点',
      validate: (source, target, sourceAnchor, targetAnchor) => {
        return !source || source.id !== START_ID;
      },
    });

    rules.push({
      message: `当前节点和目标节点已存在连接`,
      validate: (source, target) => {
        return !target?.graphModel.getNodeIncomingNode(target?.id).find(node => node.id === source?.id)
      }
    })
    return rules;
  }
}

export default {
  type: 'EndNode',
  model: EndNodeModel,
  view: RectNode,
};
