import { defineAsyncComponent as y, defineComponent as P, ref as f, reactive as w, onActivated as N, watch as O, createVNode as l, Fragment as i, resolveComponent as t, isVNode as k } from "vue";
import { ElCollapse as U, ElCollapseItem as R, ElForm as B, ElFormItem as L, ElSelect as z, ElOption as j, ElTooltip as M, ElInput as X, ElTag as J, ElButton as G, ElIcon as H, ElSwitch as K, ElInputNumber as W, ElEmpty as $, ElMessage as q, ElMessageBox as Q } from "element-plus";
import { useGetCompNode as Y, useGetCompRefProps as Z } from "@klweb/compose-app-engine";
import { j as C, a9 as ee, o as le, k as te, a8 as ae, T as c, A as u, ab as oe, ac as ne } from "./main-DXvReGBc.js";
import { e as se } from "./index-ClGUov3E.js";
const ue = y({
  loader: () => import("./DataSourceList-anO1oqx4.js"),
  loadingComponent: C
}), re = y({
  loader: () => import("./DataSourceDetails-FTMCRakr.js"),
  loadingComponent: C
}), b = {
  "ds-ie-action-icon": "_ds-ie-action-icon_y70r9_1",
  "ds-list-container": "_ds-list-container_y70r9_11",
  "ds-list-head-search": "_ds-list-head-search_y70r9_35",
  "custom-el-tag": "_custom-el-tag_y70r9_39",
  "custom-el-tag--ds": "_custom-el-tag--ds_y70r9_39",
  "header-checkbox--disabled": "_header-checkbox--disabled_y70r9_57",
  "lh-normal": "_lh-normal_y70r9_66"
}, {
  IconImport: de,
  IconExport: ce,
  IconData: ie
} = se;
function h(r) {
  return typeof r == "function" || Object.prototype.toString.call(r) === "[object Object]" && !k(r);
}
const pe = /* @__PURE__ */ P({
  name: "KlDataSourceSetter",
  components: {
    ElCollapse: U,
    ElCollapseItem: R,
    ElForm: B,
    ElFormItem: L,
    ElSelect: z,
    ElOption: j,
    ElTooltip: M,
    ElInput: X,
    ElTag: J,
    ElButton: G,
    ElIcon: H,
    ElSwitch: K,
    ElInputNumber: W,
    IconData: ie,
    ElEmpty: $,
    CommonCodeEditor: ee
  },
  props: {
    compId: {
      type: String,
      default: ""
    }
  },
  setup(r) {
    const p = f([]), m = async () => {
      const o = Y(r.compId);
      if (!o) return;
      const e = o.dataSource ? null : await Z(r.compId), n = (e == null ? void 0 : e.dataSource) || [];
      o.dataSource = o.dataSource ?? n, p.value = o.dataSource;
    };
    m();
    const _ = new le(), E = f([]), g = f(0), D = w([{
      label: "静态数据",
      value: "static"
    }, {
      label: "动态数据",
      value: "dynamic"
    }]), I = f(["json", "blob", "text", "document", "arraybuffer", "stream"]), T = (o) => {
      g.value = o;
      const e = document.getElementById("fileInput");
      e && e.click();
    }, S = (o) => {
      var n;
      const e = (n = o.target) == null ? void 0 : n.files[0];
      e && ne(e).then((s) => {
        var d, a;
        if (E.value.length) {
          const x = JSON.stringify(s) || "";
          (a = (d = E.value[g.value]) == null ? void 0 : d.editorRef) == null || a.setValue(x);
        }
      }).catch((s) => {
        q.error(s.message);
      });
    }, v = (o) => {
      _.service({
        width: "1200",
        targetFlag: "ds",
        title: u.BIND_API,
        isResize: !0,
        contentModule: ue,
        contentAttrs: {
          id: o.id
        },
        style: {
          height: "600px"
        }
      }, (e) => {
        const {
          event: n,
          emitParams: s
        } = e;
        n !== "close" || !s || (o = Object.assign(o, s));
      });
    }, V = (o) => {
      _.service({
        width: "800",
        targetFlag: "ds-details",
        title: u.VIEW_DS_DETAILS,
        isResize: !0,
        top: "8vh",
        contentModule: re,
        contentAttrs: {
          id: o
        }
      });
    }, F = (o) => {
      Q.confirm(c.DELETE_DS_WARN_TEXT, "提示", {
        type: "warning",
        draggable: !0,
        autofocus: !1
      }).then(() => {
        for (const e in o)
          o[e] = ["dsName", "type", "data"].includes(e) ? o[e] : ["abUrl", "serializeParams"].includes(e) ? !1 : e === "dataFilter" ? {
            enabled: !1,
            value: null
          } : null;
      }).catch(() => {
        console.log("取消");
      });
    }, A = () => [...Array(p.value.length).keys()];
    return N(() => m()), O([te, ae], () => {
      m();
    }), () => {
      let o;
      return l(i, null, [p.value.length ? l(t("el-collapse"), {
        class: "kl-editor-setter__collapse",
        modelValue: A()
      }, h(o = p.value.map((e, n) => {
        let s, d;
        return l(t("el-collapse-item"), {
          title: e.dsName,
          name: n
        }, {
          default: () => [l(t("el-form"), {
            class: "kl-editor-setter__form",
            model: e,
            "label-width": "100px",
            "label-position": "left"
          }, {
            default: () => [l(t("el-form-item"), {
              label: "数据源"
            }, {
              default: () => [l(t("el-select"), {
                class: "width--full",
                placeholder: c.SELECT_PLACEHOLDER,
                modelValue: e.type,
                "onUpdate:modelValue": (a) => e.type = a
              }, h(s = D.map((a) => l(t("el-option"), {
                label: a.label,
                value: a.value
              }, null))) ? s : {
                default: () => [s]
              })]
            }), e.type === "static" ? l(i, null, [l(t("el-form-item"), {
              label: "数据内容",
              class: "content-position--right"
            }, {
              default: () => [l("span", {
                class: b["ds-ie-action-icon"]
              }, [l(t("el-tooltip"), {
                effect: "dark",
                "hide-after": 0,
                placement: "top",
                content: c.IMPORT_TIP_TEXT
              }, {
                default: () => [l(t("el-button"), {
                  size: "small",
                  plain: !0,
                  icon: de,
                  onClick: () => T(n)
                }, {
                  default: () => [u.IMPORT]
                })]
              }), l(t("el-tooltip"), {
                effect: "dark",
                "hide-after": 0,
                placement: "top",
                content: c.EXPORT_TIP_TEXT
              }, {
                default: () => [l(t("el-button"), {
                  size: "small",
                  plain: !0,
                  icon: ce,
                  onClick: () => oe(e.data, e.dsName)
                }, {
                  default: () => [u.EXPORT]
                })]
              }), l("input", {
                type: "file",
                id: "fileInput",
                onChange: (a) => S(a),
                style: "display: none"
              }, null)])]
            }), l(t("common-code-editor"), {
              ref: (a) => E.value[n] = a,
              title: u.EDIT_DATA,
              height: "110px",
              modelValue: e.data,
              "onUpdate:modelValue": (a) => e.data = a,
              options: {
                lineNumbers: "off",
                folding: !1
              },
              showDialogBtnText: u.EDIT_DATA
            }, null)]) : l(i, null, [e.id ? l(i, null, [l(t("el-form-item"), {
              label: "响应数据类型"
            }, {
              default: () => [l(t("el-select"), {
                class: "width--full",
                placeholder: c.SELECT_PLACEHOLDER,
                modelValue: e.responseType,
                "onUpdate:modelValue": (a) => e.responseType = a,
                clearable: !0
              }, h(d = I.value.map((a) => l(t("el-option"), {
                label: a,
                value: a
              }, null))) ? d : {
                default: () => [d]
              })]
            }), l(t("el-form-item"), {
              label: "响应超时 (毫秒)"
            }, {
              default: () => [l(t("el-input-number"), {
                class: "width--full",
                min: 1e3,
                step: 1e3,
                modelValue: e.timeout,
                "onUpdate:modelValue": (a) => e.timeout = a
              }, null)]
            }), l(t("el-form-item"), {
              label: "序列化参数",
              class: "content-position--right"
            }, {
              default: () => [l(t("el-switch"), {
                modelValue: e.serializeParams,
                "onUpdate:modelValue": (a) => e.serializeParams = a
              }, null)]
            }), l(t("el-form-item"), {
              label: "调用绝对地址",
              class: "content-position--right"
            }, {
              default: () => [l(t("el-switch"), {
                modelValue: e.abUrl,
                "onUpdate:modelValue": (a) => e.abUrl = a
              }, null)]
            }), e.dataFilter && l(i, null, [l(t("el-form-item"), {
              label: "启用数据过滤器",
              class: "content-position--right"
            }, {
              default: () => [l(t("el-switch"), {
                modelValue: e.dataFilter.enabled,
                "onUpdate:modelValue": (a) => e.dataFilter.enabled = a
              }, null)]
            }), e.dataFilter.enabled && l(t("common-code-editor"), {
              title: u.EDIT_FILTER_FN,
              height: "110px",
              modelValue: e.dataFilter.value,
              "onUpdate:modelValue": (a) => e.dataFilter.value = a,
              language: "javascript",
              options: {
                noSemanticValidation: !1,
                lineNumbers: "off",
                folding: !1
              }
            }, null)]), l(t("el-tooltip"), {
              effect: "dark",
              "hide-after": 0,
              placement: "top",
              content: e.abUrl ? e.protocol + "://" + e.hostPort + e.url : e.url
            }, {
              default: () => [l(t("el-tag"), {
                class: b["custom-el-tag--ds"],
                type: "primary",
                size: "large",
                "disable-transitions": !0,
                closable: !0,
                onClick: () => V(e.id),
                onClose: () => F(e)
              }, {
                default: () => [e.abUrl ? e.protocol + "://" + e.hostPort + e.url : e.url]
              })]
            })]) : l(t("el-button"), {
              type: "primary",
              plain: !0,
              class: "width--full",
              onClick: () => v(e)
            }, {
              default: () => [l(t("el-icon"), {
                style: "--collapse-item-content-font-size: 14px; margin: -1px 5px 0 0"
              }, {
                default: () => [l(t("icon-data"), null, null)]
              }), u.BIND_API]
            })])]
          })]
        });
      })) ? o : {
        default: () => [o]
      }) : l(t("el-empty"), {
        "image-size": 90,
        description: c.COMP_DATA_SOURCE_NULL,
        style: "--el-empty-padding: 100px 0; --el-font-size-base: 12px"
      }, null)]);
    };
  }
}), ge = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  default: pe
}, Symbol.toStringTag, { value: "Module" }));
export {
  b as C,
  re as D,
  ge as i
};
