import { inject, computed } from 'vue';

export default function () {
  // 节点
  const node = inject('getNode');

  // 画布
  const getGraph = inject('getGraph');

  // 当前节点数据
  const getGraphNode = computed(() => {
    return getGraph.value.getNodes().find(({ id }) => node.value.id === id);
  });

  // 获取当前node数据
  const getNodeData = () => {
    return JSON.parse(getGraphNode.value.getData());
  };

  // 设置节点数据
  const setNodeData = getGraphNodeData => {
    getGraphNode.value.setData(JSON.stringify(getGraphNodeData), {
      // 必须覆盖，否则出错
      overwrite: true
    });
  };

  return {
    node,
    getGraph,
    getGraphNode,
    getNodeData,
    setNodeData
  };
}
