import { getObjByString } from "@/utils/util";

export default {
  props: {
    element: { type: Object, require: true, default: () => null },
    modeler: { type: Object, default: () => null },
    descriptor: {
      type: String,
      default: "magic",
    },
  },
  computed: {
    descriptorPrefix() {
      return `${this.descriptor}:`;
    },
  },
  created() {
    const {
      businessObject: {
        extensionElements: { values = [] } = {},
        conditionExpression,
        $attrs: attrs,
        ...rest
      } = {},
    } = this.element;
    const form = {
      ...rest,
      ...attrs,
    };

    // 解析Form表单
    if (form.form) {
      form.form = [form.form];
    }

    // 解析候选人扩展元素
    const candidateElement = values.find(
      (item) => item.$type === "magic:Candidate"
    );
    if (candidateElement) {
      const value = getObjByString(candidateElement.value);
      const candidate = value.find((item) => item.type === "user");
      if (candidate) {
        form.candidate = candidate.ids.map((id, index) => ({
          id,
          name: candidate.names[index],
        }));
      }
    }
    // 解析按钮权限扩展元素
    const btnPermissionElement = values.find(
      (item) => item.$type === "magic:Permission"
    );
    if (btnPermissionElement) {
      form.btnPermission = getObjByString(btnPermissionElement.value);
    }

    // 条件
    if (conditionExpression) {
      form.conditionType = "expression";
      form.express = conditionExpression?.body;
    }

    this.form = { ...form };
  },
  methods: {
    updateCondition(properties = {}) {
      let conditionExpression = this.element.businessObject.get(
        "conditionExpression"
      );
      if (!conditionExpression) {
        conditionExpression = this.modeler
          .get("moddle")
          .create("bpmn:FormalExpression");
      }
      Object.keys(properties).forEach((propertyName) => {
        conditionExpression[propertyName] = properties[propertyName];
      });
      this.updateProperties({
        conditionExpression,
      });
    },
    updateExtensionElementsProperties(elementType, properties) {
      let extensionElements =
        this.element.businessObject.get("extensionElements");
      if (!extensionElements) {
        extensionElements = this.modeler
          .get("moddle")
          .create("bpmn:ExtensionElements");
      }
      extensionElements.values = extensionElements.values ?? [];
      const index = extensionElements.values?.findIndex(
        (e) => e.$type === `${this.descriptorPrefix}${elementType}`
      );
      const listener =
        index === -1
          ? this.modeler
              .get("moddle")
              .create(`${this.descriptorPrefix}${elementType}`)
          : extensionElements.values[index];

      Object.keys(properties).forEach((propertyName) => {
        listener[propertyName] = properties[propertyName];
      });

      if (index === -1) {
        extensionElements.get("values").push(listener);
      } else {
        extensionElements.get("values").splice(index, 1, listener);
      }

      this.updateProperties({
        extensionElements:
          extensionElements.get("values")?.length === 0
            ? undefined
            : extensionElements,
      });
    },
    updateProperties(properties) {
      const modeling = this.modeler.get("modeling");
      modeling.updateProperties(this.element, properties);
    },
    updateProperty(propertyName, val, hasDescriptor) {
      const key = hasDescriptor
        ? `${this.descriptorPrefix}${propertyName}`
        : propertyName;
      const modeling = this.modeler.get("modeling");
      modeling.updateProperties(this.element, {
        [key]: val || undefined,
      });
    },
  },
};
