import "../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                          */
/* empty css                                                             */
/* empty css                                                                  */
import "../../../../node_modules/element-plus/theme-chalk/el-tab-pane.css.mjs";
/* empty css                                                            */
/* empty css                                                               */
/* empty css                                                             */
/* empty css                                                              */
/* empty css                                                                    */
/* empty css                                                              */
/* empty css                                                                  */
/* empty css                                                               */
/* empty css                                                               */
/* empty css                                                                     */
/* empty css                                                               */
import { getCurrentInstance, ref, watch, createElementBlock, openBlock, createVNode, unref, isRef, withCtx, createElementVNode, Fragment, renderList, createBlock, createTextVNode, createCommentVNode, toDisplayString } from "vue";
import { VideoPlay as video_play_default, CopyDocument as copy_document_default, Document as document_default } from "../../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
import ParamsConfig from "../../components/ParamsConfig.vue2.mjs";
import "../../store/index.mjs";
import { JsonViewer as Q } from "../../../../node_modules/vue3-json-viewer/dist/vue3-json-viewer.mjs";
/* empty css                                                                   */
import envConfig from "../../utils/env.mjs";
/* empty css                                                              */
/* empty css                                                                */
/* empty css                 */
import _export_sfc from "../../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElTabs, ElTabPane } from "../../../../node_modules/element-plus/es/components/tabs/index.mjs";
import { ElScrollbar } from "../../../../node_modules/element-plus/es/components/scrollbar/index.mjs";
import { ElForm, ElFormItem } from "../../../../node_modules/element-plus/es/components/form/index.mjs";
import { ElSelect, ElOption } from "../../../../node_modules/element-plus/es/components/select/index.mjs";
import { ElInput } from "../../../../node_modules/element-plus/es/components/input/index.mjs";
import { ElRadioGroup, ElRadio } from "../../../../node_modules/element-plus/es/components/radio/index.mjs";
import { ElIcon } from "../../../../node_modules/element-plus/es/components/icon/index.mjs";
import { ElButton } from "../../../../node_modules/element-plus/es/components/button/index.mjs";
import { ElTag } from "../../../../node_modules/element-plus/es/components/tag/index.mjs";
import { ElMessage } from "../../../../node_modules/element-plus/es/components/message/index.mjs";
const _hoisted_1 = { class: "data-source" };
const _hoisted_2 = { class: "config-section" };
const _hoisted_3 = { class: "desc-content" };
const _hoisted_4 = {
  key: 0,
  class: "condition-desc"
};
const _hoisted_5 = { class: "test-section" };
const _hoisted_6 = { class: "section-header" };
const _hoisted_7 = { class: "section-title" };
const _hoisted_8 = {
  key: 0,
  class: "response-area"
};
const _hoisted_9 = { class: "response-title" };
const _hoisted_10 = { class: "response-actions" };
const _hoisted_11 = { class: "response-status" };
const _hoisted_12 = { class: "response-time" };
const _hoisted_13 = {
  key: 1,
  class: "empty-response"
};
const _sfc_main = {
  __name: "dataSource",
  props: {
    requestConfigData: Object
  },
  setup(__props) {
    const currentInstance = getCurrentInstance();
    const ctx = currentInstance.proxy;
    const props = __props;
    const activeName = ref("requestConfig");
    const loading = ref(false);
    const responseData = ref("");
    const requestTime = ref(0);
    const serversOptions = ref([]);
    for (const key in envConfig.servers) {
      serversOptions.value.push({
        label: key,
        value: envConfig.servers[key]
      });
    }
    watch(
      () => props.requestConfigData,
      () => {
        responseData.value = "";
        requestTime.value = 0;
      },
      { deep: true }
    );
    async function sendRequest() {
      var _a, _b;
      const requestConfig = props.requestConfigData;
      if (!requestConfig.serverName) {
        ElMessage.warning("请选择服务");
        return;
      }
      if (!requestConfig.url) {
        ElMessage.warning("请输入请求URL");
        return;
      }
      loading.value = true;
      responseData.value = "";
      const startTime = Date.now();
      try {
        const params = {};
        (props.requestConfigData.params || []).forEach((param) => {
          if (param.key) {
            params[param.key] = param.value;
          }
        });
        const baseUrl = requestConfig.serverName ? `/${requestConfig.serverName}` : "";
        const fullUrl = `${baseUrl}${requestConfig.url}`;
        let res;
        if (requestConfig.method === "get") {
          const queryString = new URLSearchParams(params).toString();
          const url = queryString ? `${fullUrl}?${queryString}` : fullUrl;
          res = await ctx.$http.get(url, loading.value);
        } else {
          if (requestConfig.contentType === "json") {
            res = await ctx.$http.post(fullUrl, params, loading.value);
          } else {
            res = await ctx.$http.postForm(fullUrl, params, loading.value);
          }
        }
        responseData.value = {
          status: 200,
          statusText: "OK",
          data: res.data || res
        };
      } catch (error) {
        responseData.value = {
          success: false,
          error: error.message || "未知错误",
          status: ((_a = error.response) == null ? void 0 : _a.status) || 0,
          statusText: ((_b = error.response) == null ? void 0 : _b.statusText) || "网络错误"
        };
      } finally {
        requestTime.value = Date.now() - startTime;
        loading.value = false;
      }
    }
    function copyResponse() {
      var _a;
      const dataToCopy = ((_a = responseData.value) == null ? void 0 : _a.data) || responseData.value;
      navigator.clipboard.writeText(JSON.stringify(dataToCopy, null, 2)).then(() => {
        ElMessage.success("复制成功");
      }).catch(() => {
        ElMessage.error("复制失败");
      });
    }
    return (_ctx, _cache) => {
      const _component_el_option = ElOption;
      const _component_el_select = ElSelect;
      const _component_el_form_item = ElFormItem;
      const _component_el_input = ElInput;
      const _component_el_radio = ElRadio;
      const _component_el_radio_group = ElRadioGroup;
      const _component_el_form = ElForm;
      const _component_el_icon = ElIcon;
      const _component_el_button = ElButton;
      const _component_el_tag = ElTag;
      const _component_el_tab_pane = ElTabPane;
      const _component_el_scrollbar = ElScrollbar;
      const _component_el_tabs = ElTabs;
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(_component_el_tabs, {
          modelValue: unref(activeName),
          "onUpdate:modelValue": _cache[5] || (_cache[5] = ($event) => isRef(activeName) ? activeName.value = $event : null),
          class: "data-source-tab"
        }, {
          default: withCtx(() => [
            createVNode(_component_el_scrollbar, { height: "540px" }, {
              default: withCtx(() => [
                createVNode(_component_el_tab_pane, {
                  label: "请求配置",
                  name: "requestConfig"
                }, {
                  default: withCtx(() => [
                    createElementVNode("div", _hoisted_2, [
                      createVNode(_component_el_form, {
                        "label-width": "auto",
                        model: __props.requestConfigData,
                        size: "small"
                      }, {
                        default: withCtx(() => [
                          createVNode(_component_el_form_item, { label: "服务名称" }, {
                            default: withCtx(() => [
                              createVNode(_component_el_select, {
                                modelValue: __props.requestConfigData.serverName,
                                "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => __props.requestConfigData.serverName = $event),
                                placeholder: "请选择服务"
                              }, {
                                default: withCtx(() => [
                                  (openBlock(true), createElementBlock(Fragment, null, renderList(unref(serversOptions), (item) => {
                                    return openBlock(), createBlock(_component_el_option, {
                                      key: item.value,
                                      label: item.label,
                                      value: item.value
                                    }, null, 8, ["label", "value"]);
                                  }), 128))
                                ]),
                                _: 1
                              }, 8, ["modelValue"])
                            ]),
                            _: 1
                          }),
                          createVNode(_component_el_form_item, { label: "请求URL" }, {
                            default: withCtx(() => [
                              createVNode(_component_el_input, {
                                modelValue: __props.requestConfigData.url,
                                "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => __props.requestConfigData.url = $event),
                                placeholder: "请输入请求URL"
                              }, null, 8, ["modelValue"])
                            ]),
                            _: 1
                          }),
                          createVNode(_component_el_form_item, { label: "请求方式" }, {
                            default: withCtx(() => [
                              createVNode(_component_el_radio_group, {
                                modelValue: __props.requestConfigData.method,
                                "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => __props.requestConfigData.method = $event)
                              }, {
                                default: withCtx(() => [
                                  createVNode(_component_el_radio, { value: "get" }, {
                                    default: withCtx(() => _cache[6] || (_cache[6] = [
                                      createTextVNode("GET")
                                    ])),
                                    _: 1
                                  }),
                                  createVNode(_component_el_radio, { value: "post" }, {
                                    default: withCtx(() => _cache[7] || (_cache[7] = [
                                      createTextVNode("POST")
                                    ])),
                                    _: 1
                                  })
                                ]),
                                _: 1
                              }, 8, ["modelValue"])
                            ]),
                            _: 1
                          }),
                          createVNode(_component_el_form_item, { label: "内容类型" }, {
                            default: withCtx(() => [
                              createElementVNode("div", _hoisted_3, [
                                createVNode(_component_el_radio_group, {
                                  modelValue: __props.requestConfigData.contentType,
                                  "onUpdate:modelValue": _cache[3] || (_cache[3] = ($event) => __props.requestConfigData.contentType = $event)
                                }, {
                                  default: withCtx(() => [
                                    createVNode(_component_el_radio, { value: "json" }, {
                                      default: withCtx(() => _cache[8] || (_cache[8] = [
                                        createTextVNode("JSON(RequestBody)")
                                      ])),
                                      _: 1
                                    }),
                                    createVNode(_component_el_radio, { value: "formData" }, {
                                      default: withCtx(() => _cache[9] || (_cache[9] = [
                                        createTextVNode("FormData(RequestParam)")
                                      ])),
                                      _: 1
                                    }),
                                    createVNode(_component_el_radio, { value: "download" }, {
                                      default: withCtx(() => _cache[10] || (_cache[10] = [
                                        createTextVNode("Download(RequestBody)")
                                      ])),
                                      _: 1
                                    }),
                                    createVNode(_component_el_radio, { value: "upload" }, {
                                      default: withCtx(() => _cache[11] || (_cache[11] = [
                                        createTextVNode("Upload(RequestParam)")
                                      ])),
                                      _: 1
                                    })
                                  ]),
                                  _: 1
                                }, 8, ["modelValue"]),
                                __props.requestConfigData.contentType == "upload" ? (openBlock(), createElementBlock("div", _hoisted_4, " 单文件上传键名: file, 多文件上传键名: files。 ")) : createCommentVNode("", true)
                              ])
                            ]),
                            _: 1
                          }),
                          createVNode(_component_el_form_item, { label: "请求参数" }, {
                            default: withCtx(() => [
                              createVNode(ParamsConfig, {
                                modelValue: __props.requestConfigData.params,
                                "onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => __props.requestConfigData.params = $event)
                              }, null, 8, ["modelValue"])
                            ]),
                            _: 1
                          })
                        ]),
                        _: 1
                      }, 8, ["model"])
                    ]),
                    createElementVNode("div", _hoisted_5, [
                      createElementVNode("div", _hoisted_6, [
                        createElementVNode("span", _hoisted_7, [
                          createVNode(_component_el_icon, { class: "section-icon" }, {
                            default: withCtx(() => [
                              createVNode(unref(video_play_default))
                            ]),
                            _: 1
                          }),
                          _cache[12] || (_cache[12] = createTextVNode(" 请求测试 "))
                        ]),
                        createVNode(_component_el_button, {
                          size: "small",
                          type: "primary",
                          icon: unref(video_play_default),
                          loading: unref(loading),
                          onClick: sendRequest
                        }, {
                          default: withCtx(() => [
                            createTextVNode(toDisplayString(unref(loading) ? "请求中..." : "测试请求"), 1)
                          ]),
                          _: 1
                        }, 8, ["icon", "loading"])
                      ]),
                      unref(responseData) ? (openBlock(), createElementBlock("div", _hoisted_8, [
                        createElementVNode("div", _hoisted_9, [
                          _cache[14] || (_cache[14] = createElementVNode("span", null, "响应结果", -1)),
                          createElementVNode("div", _hoisted_10, [
                            createVNode(_component_el_button, {
                              size: "small",
                              text: "",
                              icon: unref(copy_document_default),
                              onClick: copyResponse
                            }, {
                              default: withCtx(() => _cache[13] || (_cache[13] = [
                                createTextVNode(" 复制 ")
                              ])),
                              _: 1
                            }, 8, ["icon"])
                          ])
                        ]),
                        createElementVNode("div", _hoisted_11, [
                          createVNode(_component_el_tag, {
                            type: unref(responseData).status >= 200 && unref(responseData).status < 300 ? "success" : "danger",
                            size: "small"
                          }, {
                            default: withCtx(() => [
                              createTextVNode(toDisplayString(unref(responseData).status) + " " + toDisplayString(unref(responseData).statusText || ""), 1)
                            ]),
                            _: 1
                          }, 8, ["type"]),
                          createElementVNode("span", _hoisted_12, "耗时: " + toDisplayString(unref(requestTime)) + "ms", 1)
                        ]),
                        createVNode(unref(Q), {
                          value: unref(responseData).data,
                          "expand-depth": 1,
                          boxed: "",
                          "show-array-index": false
                        }, null, 8, ["value"])
                      ])) : (openBlock(), createElementBlock("div", _hoisted_13, [
                        createVNode(_component_el_icon, { class: "empty-icon" }, {
                          default: withCtx(() => [
                            createVNode(unref(document_default))
                          ]),
                          _: 1
                        }),
                        _cache[15] || (_cache[15] = createElementVNode("span", null, "暂无响应数据，点击发送请求进行测试", -1))
                      ]))
                    ])
                  ]),
                  _: 1
                }),
                createVNode(_component_el_tab_pane, {
                  label: "数据处理",
                  name: "dataHandler"
                }, {
                  default: withCtx(() => _cache[16] || (_cache[16] = [
                    createTextVNode("Config")
                  ])),
                  _: 1
                })
              ]),
              _: 1
            })
          ]),
          _: 1
        }, 8, ["modelValue"])
      ]);
    };
  }
};
const DataSource = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-5c11127e"]]);
export {
  DataSource as default
};
