var q = Object.defineProperty;
var J = (r, a, i) => a in r ? q(r, a, { enumerable: !0, configurable: !0, writable: !0, value: i }) : r[a] = i;
var d = (r, a, i) => (J(r, typeof a != "symbol" ? a + "" : a, i), i);
import { EditFormModel as W } from "@ibiz-template/model";
import { useNamespace as Y, useControlController as $, usePropsWatch as _ } from "@ibiz-template/vue-util";
import { defineComponent as H, getCurrentInstance as K, ref as u, watch as Q, h as s } from "vue";
import { createUUID as V } from "qx-util";
import { ViewMode as L, PluginStaticResource as X } from "@ibiz-template/runtime";
import { EditFormController as Z } from "@ibiz-template/controller";
import { ControlVO as ee } from "@ibiz-template/service";
class te extends Z {
  constructor() {
    super(...arguments);
    /**
     *   基础数据
     *
     * @author fangZhiHao
     * @date 2024-04-26 17:04:49
     * @type {IData}
     */
    d(this, "baseResData", []);
    /**
     *   对比数据
     *
     * @author fangZhiHao
     * @date 2024-04-26 17:04:49
     * @type {IData}
     */
    d(this, "compareResData", []);
    /**
     * 表单旧数据
     *
     * @author zk
     * @date 2023-12-20 11:12:43
     * @protected
     * @type {IData}
     * @memberof FormController
     */
    d(this, "oldData", new ee());
    /**
     * version数据
     *
     * @author fangZhiHao
     * @date 2024-04-26 16:04:37
     * @type {IData[]}
     */
    d(this, "versionData", []);
    /**
     *  对比数据
     *
     * @author fangZhiHao
     * @date 2024-04-26 16:04:37
     * @type {IData[]}
     */
    d(this, "comparisonArr", {});
    /**
     *  基础表格数据
     *
     * @author fangZhiHao
     * @date 2024-05-06 11:05:57
     * @type {IData[]}
     */
    d(this, "gridData", []);
    /**
     *  展示完全数据
     *
     * @author fangZhiHao
     * @date 2024-05-06 11:05:04
     * @type {IData[]}
     */
    d(this, "AllGridData", []);
    /**
     *  去除相同数据
     *
     * @author fangZhiHao
     * @date 2024-05-06 11:05:15
     * @type {IData[]}
     */
    d(this, "removeSameGridData", []);
    /**
     *  关系界面数据
     *
     * @author fangZhiHao
     * @date 2024-04-30 10:04:33
     * @type {IData}
     */
    d(this, "AlldruipartData", /* @__PURE__ */ new Map());
    /**
     *  关系界面数据长度
     *
     * @author fangZhiHao
     * @date 2024-05-06 11:05:56
     * @type {number}
     */
    d(this, "druipartDataSize", 0);
    /**
     *   关系界面实际数据
     *
     * @author fangZhiHao
     * @date 2024-05-06 11:05:13
     * @type {IData}
     */
    d(this, "newDruipartData", {});
  }
  /**
   * 加载数据
   *
   * @return {*}  {Promise<ControlVO>}
   * @memberof FormDataComparisonController
   */
  async loadGridData() {
    await this.startLoading();
    const i = this.model.source.M.getPSControlParam.ctrlParams, { BASEENTITY: o, COMPAREENTITY: h } = i, E = await ibiz.entityService.getService(o), v = await ibiz.entityService.getService(h), f = Object.assign({}, this.context.clone(), {
      [o.toLowerCase()]: this.params.base_version
    }), y = Object.assign({}, this.context.clone(), {
      [h.toLowerCase()]: this.params.compare_version
    }), w = await E.exec("Get", f, this.params), D = await v.exec(
      "Get",
      y,
      this.params
    );
    return this.isLoaded = !0, {
      baseData: w.data.data,
      compareData: D.data.data
    };
  }
  /**
   * 判断值是否为空
   *
   * @param {(string | IData[] | null | undefined)} value
   * @return {*}
   * @memberof FormDataComparisonController
   */
  isFalseyButNotEmpty(i) {
    return i == null || typeof i == "string" && i === "" || Array.isArray(i) && i.length === 0 || typeof i == "object" && Object.keys(i).length === 0;
  }
  /**
   * 比较两个值
   *
   * @param {IData} objA
   * @param {IData} objB
   * @return {*}
   * @memberof FormDataComparisonController
   */
  compareObjectValues(i, o) {
    return JSON.stringify(i) !== JSON.stringify(o);
  }
}
const se = H({
  name: "IBizFormDataComparison",
  props: {
    modelData: {
      type: W,
      required: !0
    },
    context: {
      type: Object,
      required: !0
    },
    params: {
      type: Object,
      default: () => ({})
    },
    enableAutoSave: {
      type: Boolean,
      required: !1
    }
  },
  setup(r) {
    const a = Y("form-data-comparison"), {
      proxy: i
    } = K(), o = $(i, () => {
      const e = new te(r.modelData, r.context, r.params);
      return _(i, "enableAutoSave", e.setEnableAutoSave.bind(e)), e;
    }), h = u(!0), E = u(!1), v = u({}), f = u({}), y = u([]), w = [], D = u([]), B = u([]), P = u([]), j = /* @__PURE__ */ new Map(), I = u(0), x = (e) => {
      const t = [];
      if (t.push(e), e && e.children) {
        const n = [];
        e.children.forEach((c) => {
          const m = x(c);
          n.push(...m);
        }), t.push(...n);
      }
      return t;
    }, z = x(r.modelData);
    w.push(...z);
    const O = (e) => {
      const t = o.details[e.source.name], n = s("not-supported-editor", {
        attrs: {
          modelData: e.editor
        }
      });
      return s("div", {
        class: [a.e("form-grid-item"), a.is("add", D.value.includes(e.source.name)), a.is("update", P.value.includes(e.source.name)), a.is("delete", B.value.includes(e.source.name))]
      }, [s("div", {
        class: a.em("form-grid-item", "name")
      }, [e.source.caption]), s("div", {
        class: a.em("form-grid-item", "left")
      }, [t.editorProvider ? s(t.editorProvider.formEditor, {
        props: {
          value: f.value[e.source.name],
          data: t.data,
          controller: t.editor,
          disabled: t.disabled,
          readonly: !0
        }
      }) : n]), s("div", {
        class: a.em("form-grid-item", "right")
      }, [t.editorProvider ? s(t.editorProvider.formEditor, {
        props: {
          value: v.value[e.source.name],
          data: t.data,
          controller: t.editor,
          disabled: t.disabled,
          readonly: !0
        }
      }) : n])]);
    }, S = (e, t) => {
      j.set(e.source.id, t), t.evt.on("mounted", () => {
        h.value && o.startLoading(), I.value += 1, t.call.refresh();
      }), t.evt.on("refreshed", () => {
        I.value -= 1, setTimeout(() => {
          h.value && o.endLoading();
        }, 500);
      });
    }, G = (e) => {
      const t = w.find((b) => b.id === `${e.source.id}_srfcopy`), n = e.embedView.source.modelPath, c = t == null ? void 0 : t.embedView.source.modelPath, m = o.context.clone(), l = {
        base_version: r.params.base_version
      }, p = o.context.clone();
      Object.assign(p, {
        gysrlzy_info: r.params.compare_version,
        compare_version: r.params.compare_version
      });
      const g = {
        compare_version: r.params.compare_version
      }, F = v.value[e.source.userTag], C = f.value[t == null ? void 0 : t.source.userTag];
      let M = !1, A = !1, R = !1;
      return o.isFalseyButNotEmpty(F) && !o.isFalseyButNotEmpty(C) ? M = !0 : !o.isFalseyButNotEmpty(F) && o.isFalseyButNotEmpty(C) ? R = !0 : !o.isFalseyButNotEmpty(F) && !o.isFalseyButNotEmpty(C) && o.compareObjectValues(F, C) && (A = !0), s("div", {
        class: [a.e("druipart"), a.is("add", M), a.is("update", A), a.is("delete", R)]
      }, [s("div", {
        class: a.em("druipart", "caption")
      }, [e.source.caption]), s("div", {
        class: a.em("druipart", "left")
      }, [c && s("ViewShell", {
        attrs: {
          context: p,
          params: g,
          modal: {
            mode: L.EMBED
          },
          modelPath: c,
          noLoadDefault: !0,
          name: t == null ? void 0 : t.source.id
        },
        key: V(),
        on: {
          neuronInit: (b) => S(t, b)
        }
      })]), s("div", {
        class: a.em("druipart", "right")
      }, [n && s("ViewShell", {
        attrs: {
          context: m,
          params: l,
          modal: {
            mode: L.EMBED
          },
          modelPath: n,
          noLoadDefault: !0,
          name: e.source.id
        },
        key: V(),
        on: {
          neuronInit: (b) => S(e, b)
        }
      })])]);
    }, U = (e) => {
      const t = {
        modelData: e,
        controller: o.details[e.source.name]
      }, n = o.providers[e.source.name], c = s(n.component, {
        props: {
          ...t
        },
        key: e.id
      }), m = s(n.component, {
        props: {
          ...t
        },
        key: `compare_${e.id}`
      });
      return s("div", {
        class: a.e("mdctrl")
      }, [s("div", {
        class: a.em("mdctrl", "caption")
      }, [e.source.caption]), s("div", {
        class: a.em("mdctrl", "left")
      }, [c]), s("div", {
        class: a.em("mdctrl", "right")
      }, [m])]);
    }, T = (e) => {
      h.value = !1;
      const t = y.value.findIndex((n) => n === e.source.name);
      t > -1 ? y.value.splice(t, 1) : y.value.push(e.source.name);
    }, N = (e) => {
      if (!e.source.hidden) {
        if (e.source.detailType === "FORMPAGE")
          return s("div", {
            class: a.e("form-page")
          }, [e.children.map((t) => N(t))]);
        if (e.source.detailType === "FORMITEM") {
          const t = D.value.includes(e.source.name), n = B.value.includes(e.source.name), c = P.value.includes(e.source.name);
          return E.value && !t && !n && !c ? null : O(e);
        }
        if (e.source.detailType === "GROUPPANEL") {
          const t = y.value.includes(e.source.name);
          return s("div", {
            class: a.b("group")
          }, [s("div", {
            class: [a.be("group", "header"), a.is("hide-caption", !e.source.showCaption)]
          }, [s("div", {
            class: a.bem("group", "header", "name")
          }, [t ? s("i-icon", {
            class: a.bem("group", "header", "icon"),
            attrs: {
              type: "ios-arrow-forward",
              title: "展开"
            },
            on: {
              click: () => T(e)
            }
          }) : s("i-icon", {
            class: a.bem("group", "header", "icon"),
            attrs: {
              type: "ios-arrow-down",
              title: "收缩"
            },
            on: {
              click: () => T(e)
            }
          }), s("span", {
            class: a.bem("group", "header", "name-text")
          }, [e.source.caption || e.source.title])]), s("div", ["---"]), s("div", ["---"])]), s("div", {
            class: [a.be("group", "content"), a.is("collapse", t)]
          }, [e.children.map((n) => N(n))])]);
        }
        return e.source.detailType === "DRUIPART" && !e.source.name.endsWith("_srfcopy") ? G(e) : e.source.detailType === "MDCTRL" ? U(e) : null;
      }
    }, k = (e, t) => {
      const n = [], c = [], m = [];
      return Object.keys(t).forEach((l) => {
        const p = e[l], g = t[l];
        o.isFalseyButNotEmpty(p) && !o.isFalseyButNotEmpty(g) && n.push(l), !o.isFalseyButNotEmpty(p) && !o.isFalseyButNotEmpty(g) && o.compareObjectValues(p, g) && c.push(l);
      }), Object.keys(e).forEach((l) => {
        if (!Object.prototype.hasOwnProperty.call(t, l) || o.isFalseyButNotEmpty(t[l])) {
          const p = e[l];
          o.isFalseyButNotEmpty(p) || m.push(l);
        }
      }), {
        added: n,
        modified: c,
        deleted: m
      };
    };
    return Q(() => r.params, async () => {
      const e = await o.loadGridData();
      v.value = e.baseData, f.value = e.compareData;
      const {
        added: t,
        modified: n,
        deleted: c
      } = k(f.value, v.value);
      D.value = t, P.value = n, B.value = c;
    }, {
      immediate: !0
    }), {
      ns: a,
      c: o,
      switchValue: E,
      druipartNum: I,
      renderEditor: O,
      renderByDetailType: N
    };
  },
  render() {
    return this.c.complete ? s("control-layout", {
      attrs: {
        modelData: this.c.model
      }
    }, [s("div", {
      class: this.ns.b()
    }, [s("div", {
      class: this.ns.e("top")
    }, [s("div", {
      class: this.ns.e("top-hidesame-content")
    }, [s("div", {
      class: this.ns.e("top-hidesame-content-left")
    }, ["隐藏相同项 ", s("i-switch", {
      model: {
        value: this.switchValue,
        callback: (r) => {
          this.switchValue = r;
        }
      }
    })]), s("div", {
      class: this.ns.e("top-hidesame-content-right")
    }, [s("div", {
      class: this.ns.e("top-hidesame-content-right-item")
    }, ["新增", s("div", {
      class: this.ns.e("top-hidesame-content-right-item-add")
    })]), s("div", {
      class: this.ns.e("top-hidesame-content-right-item")
    }, ["修改", s("div", {
      class: this.ns.e("top-hidesame-content-right-item-change")
    })]), s("div", {
      class: this.ns.e("top-hidesame-content-right-item")
    }, ["删除", s("div", {
      class: this.ns.e("top-hidesame-content-right-item-delete")
    })])])]), s("div", {
      class: this.ns.e("top-content")
    }, [s("div", {
      class: this.ns.em("top-content", "attr")
    }, ["信息项"]), s("div", {
      class: [this.ns.em("top-content", "left")]
    }, ["更新前内容"]), s("div", {
      class: [this.ns.em("top-content", "right")]
    }, ["更新后内容"])])]), s("div", {
      class: this.ns.e("content")
    }, [this.modelData.children.map((r) => this.renderByDetailType(r))])])]) : null;
  }
});
class ae {
  constructor() {
    d(this, "component", "IBizFormDataComparison");
  }
}
const oe = new X(import.meta.url), pe = {
  mateUrl: import.meta.url,
  install(r) {
    oe.loadStyle(["/style.css"]), r.component("IBizFormDataComparison", se), ibiz.register.control.register(
      "EDITFORM_RENDER_FORM_DATA_COMPARISON",
      new ae()
    );
  }
};
export {
  pe as default
};
