export default {
  props: {
    compareHData1: {
      type: Object,
      default: () => ({}),
    },
    compareHData2: {
      type: Object,
      default: () => ({}),
    },
    preVersion: [String, Number],
    curVersion: [String, Number],
    objType: String,
    objCode: String,
  },
  methods: {
    isNull(val) {
      return val == null || val === "" || val === undefined;
    },
    isDiff(val1, val2) {
      if (this.isNull(val1) && this.isNull(val2)) return false;
      return val1 !== val2;
    },
    rowDiffClass(val1, val2) {
      return this.isDiff(val1, val2) ? "id-difference" : "";
    },
    formatBool(val, trueText = "是", falseText = "否") {
      if (val === true || val === 1) return this.$t1(trueText);
      if (val === false || val === 0) return this.$t1(falseText);
      return val ?? "";
    },
    formatTags(tags) {
      if (!tags || !tags.length) return "";
      return tags
        .map((item) => item.tagName)
        .filter(Boolean)
        .join(", ");
    },
    formatDataRange(val) {
      if (val === 1) return this.$t1("集团");
      if (val === 0) return this.$t1("当前组织");
      return val ?? "";
    },
    formatFormCode(val) {
      if (val === "intf") return this.$t1("intf(接口)");
      if (val === "busgeneral") return this.$t1("busgeneral(业务通用)");
      return val ?? "";
    },
    formatScriptUrlPath(val, data) {
      if (!data) return "";
      return `${data.formCode || ""}/${data.scriptCode || ""}`;
    },
    formatJson(content) {
      if (this.isNull(content)) return "";
      if (typeof content === "object") {
        try {
          return JSON.stringify(content, null, 2);
        } catch (e) {
          return String(content);
        }
      }
      const text = String(content).trim();
      if (!text) return "";
      try {
        return JSON.stringify(JSON.parse(text), null, 2);
      } catch (e) {
        return text;
      }
    },
    auditFields() {
      return [
        { key: "createBy", label: "创建人" },
        { key: "createDate", label: "创建时间" },
        { key: "modifyBy", label: "更新人" },
        { key: "modifyDate", label: "更新时间" },
      ];
    },
    getFieldVal(data, field) {
      if (!data) return "";
      let val = data[field.key];
      if (field.formatter) {
        return field.formatter(val, data);
      }
      return val ?? "";
    },
    fieldDiffClass(field) {
      return this.rowDiffClass(
        this.getFieldVal(this.compareHData1, field),
        this.getFieldVal(this.compareHData2, field)
      );
    },
  },
};
