// 贝塞尔曲线
import { BezierEdge, BezierEdgeModel } from '@logicflow/core';
import { randomNumber } from '@/utils/index';

class BezierModel extends BezierEdgeModel {
  createId() {
    return randomNumber();
  }

  constructor(data, graphModel) {
    
    super(data, graphModel);
    console.log(graphModel, 'edge-constructor-graphModel');
    console.log(this, 'edge-constructor');
    const node = graphModel.getNodeModelById(this.sourceNodeId);
    const childNode = graphModel.getNodeModelById(this.targetNodeId);
    if (this.sourceAnchorId?.endsWith('_customAnchor')) {
      
      if (node?.anchors) {
        const anchor = node.anchors.find((item) => item.id === this.sourceAnchorId);
        console.log(anchor, 'anchor');
        if (anchor.text) this.updateText(anchor.text);
        if (anchor?.outParams) {
          this.setProperties({
            ...this.getProperties(),
            outParams: anchor.outParams,
            sourceAnchorId: this.sourceAnchorId.split('_')[0],
          });
        }
        
      }
      
    }
    const parentParoperties = node.getProperties();
    if (parentParoperties.outParams) {
      childNode.setProperties({
        ...childNode.getProperties(),
        inputParams: parentParoperties.outParams,
      });
    }

    
    // 这种右键操作菜单
    // this.menu = [
    //   {
    //     text: '配置',
    //     callback: (res) => {
    //       graphModel.eventCenter.emit('edge:app-config', res); //发出事件
    //     },
    //   },
    //   {
    //     text: '删除',
    //     callback(res) {
    //       graphModel.deleteEdgeById(res.id);
    //     },
    //   },
    // ];
  }
  initEdgeData(data) {
    console.log(data, 'initEdgeData');
    super.initEdgeData(data);
  }
}

export default {
  type: 'myBezier',
  view: BezierEdge,
  model: BezierModel,
};
