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

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

  getConnectedSourceRules() {
    const rules = super.getConnectedSourceRules();
    rules.push({
      message: '开始节点仅允许1个输出',
      validate: (source) => {
        const edge = this.graphModel.getNodeOutgoingEdge(source!.id);
        return !edge.length;
      },
    });
    return rules;
  }

  getConnectedTargetRules() {
    const rules = super.getConnectedTargetRules();
    rules.push({
      message: '开始节点不允许输入',
      validate: () => false,
    });
    return rules;
  }
}

export default {
  type: 'StartNode',
  model: StartNodeModel,
  view: RectNode,
};
