import { defineComponent, useAttrs, ref, watch, resolveComponent, createBlock, openBlock, mergeProps, unref, createSlots, withCtx, createVNode, createElementVNode, toDisplayString, normalizeClass, createElementBlock, createCommentVNode } from 'vue';
import '../../../utils/index.mjs';
import { Form } from 'ant-design-vue';
import { getHttpMethodUri, httpUriOptions, uriInputEmits, uriInputProps } from './uri-input.mjs';
import { createNamespace } from '../../../utils/create-namespace.mjs';

"use strict";
const _hoisted_1 = { key: 0 };
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{ name: "LlUriInput" },
  __name: "uri-input",
  props: uriInputProps,
  emits: uriInputEmits,
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const attrs = useAttrs();
    const formItemContext = Form.useInjectFormItemContext();
    const bem = createNamespace("uri-input");
    const copyText = ref("");
    const innerValue = ref("");
    const innerPrefix = 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);
      }
    };
    watch(
      () => props.value,
      (val) => {
        const safeValue = convertToString(val);
        const { prefix, url } = 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
      }
    );
    watch(
      innerValue,
      () => {
        handleChange();
      },
      {
        immediate: true
      }
    );
    watch(
      () => props.addonAfter,
      () => {
        handleChange();
      }
    );
    return (_ctx, _cache) => {
      const _component_ll_select = resolveComponent("ll-select");
      const _component_ll_icon = resolveComponent("ll-icon");
      const _component_ll_copy_text = resolveComponent("ll-copy-text");
      const _component_ll_input = resolveComponent("ll-input");
      return openBlock(), createBlock(_component_ll_input, mergeProps({
        value: innerValue.value,
        "onUpdate:value": _cache[1] || (_cache[1] = ($event) => innerValue.value = $event)
      }, unref(attrs), {
        class: [unref(bem).b()],
        disabled: props.disabled,
        placeholder: props.placeholder,
        onBlur: handleBlur
      }), createSlots({
        addonBefore: withCtx(() => [
          createVNode(_component_ll_select, {
            value: innerPrefix.value,
            "onUpdate:value": _cache[0] || (_cache[0] = ($event) => innerPrefix.value = $event),
            options: unref(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: withCtx(() => [
            createElementVNode(
              "span",
              null,
              toDisplayString(props.suffix),
              1
              /* TEXT */
            )
          ]),
          key: "0"
        } : void 0,
        props.addonAfter || _ctx.showCopy ? {
          name: "addonAfter",
          fn: withCtx(() => [
            createElementVNode(
              "div",
              {
                class: normalizeClass(unref(bem).e("addon-after"))
              },
              [
                props.addonAfter ? (openBlock(), createElementBlock(
                  "span",
                  _hoisted_1,
                  toDisplayString(_ctx.addonAfter),
                  1
                  /* TEXT */
                )) : createCommentVNode("v-if", true),
                _ctx.showCopy ? (openBlock(), createBlock(_component_ll_copy_text, {
                  key: 1,
                  value: copyText.value
                }, {
                  value: withCtx(() => [
                    createVNode(_component_ll_icon, { "icon-name": "icon-copy" })
                  ]),
                  _: 1
                  /* STABLE */
                }, 8, ["value"])) : createCommentVNode("v-if", true)
              ],
              2
              /* CLASS */
            )
          ]),
          key: "1"
        } : void 0
      ]), 1040, ["value", "class", "disabled", "placeholder"]);
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=uri-input.vue2.mjs.map
