import { getCurrentInstance } from "vue";
import BaseRenderer from "../../../../../../../../node_modules/diagram-js/lib/draw/BaseRenderer.mjs";
import _sfc_main from "../component/NodeItem.vue.mjs";
import renderComponent from "./renderComponent.mjs";
import { getRoundRectPath } from "../../../../../../../../node_modules/bpmn-js/lib/draw/BpmnRenderUtil.mjs";
import { isAny, is } from "../../../../../../../../node_modules/bpmn-js/lib/util/ModelUtil.mjs";
const HIGH_PRIORITY = 1500, TASK_BORDER_RADIUS = 2;
class CustomRenderer extends BaseRenderer {
  constructor(eventBus, bpmnRenderer) {
    super(eventBus, HIGH_PRIORITY);
    this.bpmnRenderer = bpmnRenderer;
  }
  canRender(element) {
    return isAny(element, [
      "bpmn:Task",
      "bpmn:Event",
      "bpmn:Gateway",
      "bpmn:CallActivity",
      "bpmn:Transaction"
    ]);
  }
  drawShape(parentNode, element) {
    if (element.isReResize) {
      delete element.isReResize;
      return;
    }
    if (element.labelTarget) {
      element.y += 10;
      return;
    }
    let name = element.businessObject.$attrs["normal:name"];
    if (name) {
      try {
        name = JSON.parse(name)["zh_CN"];
      } catch (error) {
      }
      element.businessObject.name = name;
    }
    appendCustomDom(parentNode, element);
    parentNode.remove();
  }
  getShapePath(shape) {
    if (is(shape, "bpmn:Task")) {
      return getRoundRectPath(shape, TASK_BORDER_RADIUS);
    }
    return this.bpmnRenderer.getShapePath(shape);
  }
}
CustomRenderer.$inject = ["eventBus", "bpmnRenderer"];
const appendCustomDom = (parentNode, element) => {
  let svgDom = document.querySelector(
    ".djs-element[data-element-id='" + element.id + "']"
  );
  if (svgDom) {
    let oldForeignObject = svgDom.querySelector("foreignObject");
    if (oldForeignObject) {
      oldForeignObject.remove();
    }
  }
  let id = "node-item-" + element.id;
  let foreignObject = document.createElementNS(
    "http://www.w3.org/2000/svg",
    "foreignObject"
  );
  let nodeType = element.businessObject.$attrs["normal:nodeType"];
  if (!nodeType) {
    if (element.type == "bpmn:StartEvent") {
      nodeType = "start";
      element.width = 32;
      element.height = 32;
    } else if (element.type == "bpmn:EndEvent") {
      nodeType = "end";
      element.width = 32;
      element.height = 32;
    } else if (element.type == "bpmn:ExclusiveGateway") {
      nodeType = "gateway";
    } else if (element.type == "bpmn:ParallelGateway") {
      nodeType = "parallelGateway";
      if (element.id.endsWith("mergeGateway")) {
        nodeType = "mergeGateway";
      }
    } else if (element.type == "bpmn:InclusiveGateway") {
      nodeType = "inclusiveGateway";
    } else if (element.type == "bpmn:ServiceTask") {
      nodeType = "service";
      let delegateExpression = element.businessObject.$attrs["flowable:delegateExpression"];
      if (delegateExpression && delegateExpression == "${flowRuleListener}") {
        nodeType = "rde";
      }
    } else if (element.type == "bpmn:ReceiveTask") {
      nodeType = "wait";
    } else if (element.type == "bpmn:CallActivity") {
      nodeType = "childProcess";
    }
  }
  foreignObject.setAttribute("id", id);
  foreignObject.setAttribute("width", element.width);
  foreignObject.setAttribute("height", element.height);
  foreignObject.setAttribute("class", "custom-foreignObject");
  let div = document.createElement("div");
  div.setAttribute("class", "custom-node-item ");
  parentNode.parentNode.appendChild(foreignObject);
  foreignObject.appendChild(div);
  let apidata = window.flowChartData;
  let timeLineData = apidata.timeLineData;
  let taskArr = timeLineData.task;
  let hisTaskArr = timeLineData.hisTask;
  let isCurrent = false;
  let currNode = apidata.activeActivityIds.find((item) => item === element.id);
  if (currNode) {
    isCurrent = true;
  }
  let isHighlight = false;
  let data = taskArr.find((item) => item.taskDefinitionKey == element.id);
  if (data) {
    isHighlight = true;
  } else {
    data = hisTaskArr.find((item) => item.taskDefinitionKey == element.id);
    if (data) {
      isHighlight = true;
    }
  }
  if (!timeLineData.isEnd && element.type == "bpmn:StartEvent") {
    isHighlight = true;
  } else if (timeLineData.isEnd) {
    if (element.type == "bpmn:StartEvent" || element.type == "bpmn:EndEvent") {
      isHighlight = true;
    }
  }
  let hisTaskList = [];
  hisTaskArr.forEach((item) => {
    if (item.taskDefinitionKey == element.id) {
      hisTaskList.push(item);
    }
  });
  let taskList = [];
  taskArr.forEach((item) => {
    if (item.taskDefinitionKey == element.id) {
      taskList.push(item);
    }
  });
  renderComponent({
    el: div,
    //  组件要插入的那个dom
    component: _sfc_main,
    // 要插入的组件
    props: {
      // 传入 props
      nodeType,
      isHighlight,
      hisTaskList,
      taskList,
      isCurrent
    },
    appContext: getCurrentInstance()
    // 获取组件实例的
  });
};
export {
  CustomRenderer as default
};
