<template>
  <view :class="popoutInputClass" :style="popoutInputStyle">
    <sar-input
      inlaid
      :model-value="innerValue"
      @update:model-value="onChange"
      :placeholder="placeholder"
      :readonly="isReadonly"
      :disabled="isDisabled"
      :clearable="clearable"
      @clear="onClear"
      :validate-event="false"
    >
      <template #append>
        <view :class="bem.e('append')">
          <view :class="bem.e('loading')" v-if="loading">
            <sar-loading />
          </view>
          <view :class="bem.e('arrow')">
            <sar-icon name="caret-right" />
          </view>
        </view>
      </template>
      <template #addon>
        <view
          :class="bem.e('seal')"
          @mousedown="onSealMouseDown"
          @touchstart="onSealTouchStart"
          @touchend="onSealTouchEnd"
          @touchcancel="onSealTouchEnd"
          @click="onClick"
        />
      </template>
    </sar-input>
  </view>
</template>

<script lang="ts">
import { defineComponent as _defineComponent } from "vue";
import { computed, ref, watch } from "vue";
import { classNames, stringifyStyle, createBem } from "../utils";
import SarInput from "../input/input.vue";
import SarIcon from "../icon/icon.vue";
import { useFormContext, useFormItemContext } from "../form/common";
import { popoutInputProps } from "./common";
import SarLoading from "../loading/loading.vue";
const __default__ = {
  options: {
    virtualHost: true,
    styleIsolation: "shared"
  }
};
export default /* @__PURE__ */ _defineComponent({
  ...__default__,
  __name: "popout-input",
  props: popoutInputProps,
  emits: ["click", "update:model-value", "clear"],
  setup(__props, { expose: __expose, emit: __emit }) {
    __expose();
    const props = __props;
    const emit = __emit;
    const bem = createBem("popout-input");
    const formContext = useFormContext();
    const formItemContext = useFormItemContext();
    const isDisabled = computed(() => {
      return formContext?.disabled ?? props.disabled;
    });
    const isReadonly = computed(() => {
      return formContext?.readonly ?? props.readonly;
    });
    const innerValue = ref(props.modelValue);
    const setInnerValue = (value) => {
      innerValue.value = value;
      emit("update:model-value", value);
    };
    watch(
      () => props.modelValue,
      () => {
        innerValue.value = props.modelValue;
        if (props.validateEvent) {
          formItemContext?.onChange();
        }
      }
    );
    const onChange = (value) => {
      setInnerValue(value);
    };
    const onClear = () => {
      setInnerValue("");
      emit("clear");
    };
    const isSealDown = ref(false);
    const interoperable = computed(() => {
      return !isDisabled.value && !isReadonly.value && !props.loading;
    });
    const onSealTouchStart = () => {
      if (interoperable.value) {
        isSealDown.value = true;
      }
    };
    const onSealTouchEnd = () => {
      if (interoperable.value) {
        isSealDown.value = false;
      }
    };
    const onSealMouseDown = () => {
      onSealTouchStart();
      const upHandler = () => {
        onSealTouchEnd();
        document.removeEventListener("mouseup", upHandler);
      };
      document.addEventListener("mouseup", upHandler);
    };
    const onClick = (event) => {
      if (interoperable.value) {
        emit("click", event);
      }
    };
    const popoutInputClass = computed(() => {
      return classNames(
        bem.b(),
        bem.m("down", isSealDown.value),
        bem.m("loading", props.loading),
        bem.m("readonly", isReadonly.value),
        bem.m("disabled", isDisabled.value),
        props.rootClass
      );
    });
    const popoutInputStyle = computed(() => {
      return stringifyStyle(props.rootStyle);
    });
    const __returned__ = { props, emit, bem, formContext, formItemContext, isDisabled, isReadonly, innerValue, setInnerValue, onChange, onClear, isSealDown, interoperable, onSealTouchStart, onSealTouchEnd, onSealMouseDown, onClick, popoutInputClass, popoutInputStyle, SarInput, SarIcon, SarLoading };
    return __returned__;
  }
});
</script>

<style lang="scss">
@use './index.scss';
</style>