'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.js');
var antDesignVue = require('ant-design-vue');
var uriInput = require('./uri-input.js');
var createNamespace = require('../../../utils/create-namespace.js');

"use strict";
const _hoisted_1 = { key: 0 };
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{ name: "LlUriInput" },
  __name: "uri-input",
  props: uriInput.uriInputProps,
  emits: uriInput.uriInputEmits,
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const attrs = vue.useAttrs();
    const formItemContext = antDesignVue.Form.useInjectFormItemContext();
    const bem = createNamespace.createNamespace("uri-input");
    const copyText = vue.ref("");
    const innerValue = vue.ref("");
    const innerPrefix = vue.ref("");
    const handleChange = () => {
      const url = innerValue.value ? innerPrefix.value + innerValue.value + (props.suffix || "") + (props.addonAfter || "") : "";
      copyText.value = url ? url : innerPrefix.value;
      emits("update:value", url);
      emits("change", url);
      formItemContext.onFieldChange();
    };
    const handleBlur = (e) => {
      if (attrs.onBlur && typeof attrs.onBlur === "function") {
        attrs.onBlur(e);
      }
      formItemContext.onFieldBlur();
    };
    const convertToString = (value) => {
      if (value === null || value === void 0) {
        return "";
      }
      if (typeof value === "string") {
        return value;
      }
      if (value instanceof Date) {
        return value.toISOString();
      }
      if (typeof value === "number" || typeof value === "boolean") {
        return String(value);
      }
      try {
        return JSON.stringify(value);
      } catch {
        return String(value);
      }
    };
    vue.watch(
      () => props.value,
      (val) => {
        const safeValue = convertToString(val);
        const { prefix, url } = uriInput.getHttpMethodUri(safeValue);
        let currentUrl = url;
        if (url.includes(prefix)) {
          currentUrl = currentUrl.replace(prefix, "");
        }
        innerPrefix.value = prefix;
        innerValue.value = currentUrl;
        copyText.value = innerValue.value ? prefix + innerValue.value : prefix;
        if (props.suffix) {
          innerValue.value = url.replace(props.suffix, "");
        }
        if (props.addonAfter) {
          innerValue.value = url.replace(props.addonAfter, "");
        }
      },
      {
        immediate: true
      }
    );
    vue.watch(
      innerValue,
      () => {
        handleChange();
      },
      {
        immediate: true
      }
    );
    vue.watch(
      () => props.addonAfter,
      () => {
        handleChange();
      }
    );
    return (_ctx, _cache) => {
      const _component_ll_select = vue.resolveComponent("ll-select");
      const _component_ll_icon = vue.resolveComponent("ll-icon");
      const _component_ll_copy_text = vue.resolveComponent("ll-copy-text");
      const _component_ll_input = vue.resolveComponent("ll-input");
      return vue.openBlock(), vue.createBlock(_component_ll_input, vue.mergeProps({
        value: innerValue.value,
        "onUpdate:value": _cache[1] || (_cache[1] = ($event) => innerValue.value = $event)
      }, vue.unref(attrs), {
        class: [vue.unref(bem).b()],
        disabled: props.disabled,
        placeholder: props.placeholder,
        onBlur: handleBlur
      }), vue.createSlots({
        addonBefore: vue.withCtx(() => [
          vue.createVNode(_component_ll_select, {
            value: innerPrefix.value,
            "onUpdate:value": _cache[0] || (_cache[0] = ($event) => innerPrefix.value = $event),
            options: vue.unref(uriInput.httpUriOptions),
            style: { "min-width": "90px" },
            disabled: props.disabled,
            "allow-clear": false,
            onChange: handleChange
          }, null, 8, ["value", "options", "disabled"])
        ]),
        _: 2
        /* DYNAMIC */
      }, [
        props.suffix ? {
          name: "suffix",
          fn: vue.withCtx(() => [
            vue.createElementVNode(
              "span",
              null,
              vue.toDisplayString(props.suffix),
              1
              /* TEXT */
            )
          ]),
          key: "0"
        } : void 0,
        props.addonAfter || _ctx.showCopy ? {
          name: "addonAfter",
          fn: vue.withCtx(() => [
            vue.createElementVNode(
              "div",
              {
                class: vue.normalizeClass(vue.unref(bem).e("addon-after"))
              },
              [
                props.addonAfter ? (vue.openBlock(), vue.createElementBlock(
                  "span",
                  _hoisted_1,
                  vue.toDisplayString(_ctx.addonAfter),
                  1
                  /* TEXT */
                )) : vue.createCommentVNode("v-if", true),
                _ctx.showCopy ? (vue.openBlock(), vue.createBlock(_component_ll_copy_text, {
                  key: 1,
                  value: copyText.value
                }, {
                  value: vue.withCtx(() => [
                    vue.createVNode(_component_ll_icon, { "icon-name": "icon-copy" })
                  ]),
                  _: 1
                  /* STABLE */
                }, 8, ["value"])) : vue.createCommentVNode("v-if", true)
              ],
              2
              /* CLASS */
            )
          ]),
          key: "1"
        } : void 0
      ]), 1040, ["value", "class", "disabled", "placeholder"]);
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=uri-input.vue2.js.map
