import { defineComponent, useModel, useSlots, ref, computed, watch, resolveComponent, createBlock, openBlock, mergeProps, createSlots, withCtx, unref, renderSlot, normalizeProps, guardReactiveProps, mergeModels } from 'vue';
import { uploadItemProps } from './uploadItem.mjs';

var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "GmUploadItem"
  },
  __name: "UploadItem",
  props: /* @__PURE__ */ mergeModels(uploadItemProps, {
    "imageUrl": { required: true },
    "imageUrlModifiers": {}
  }),
  emits: ["update:imageUrl"],
  setup(__props) {
    const props = __props;
    const imageUrl = useModel(__props, "imageUrl");
    const slots = useSlots();
    const fileList = ref([]);
    const uploadDisabled = computed(() => Boolean(props.disabled) || fileList.value.length > 0);
    watch(
      imageUrl,
      (newVal) => {
        if (newVal) {
          fileList.value = [{ url: newVal, name: newVal }];
        } else {
          fileList.value = [];
        }
      },
      { immediate: true }
    );
    const handleComplete = () => {
      imageUrl.value = fileList.value[0]?.response?.url ?? "";
      props.onAllComplete?.();
    };
    const handleRemove = (file, uploadFiles) => {
      imageUrl.value = "";
      props.onRemove?.(file, uploadFiles);
    };
    return (_ctx, _cache) => {
      const _component_gm_upload_pro = resolveComponent("gm-upload-pro");
      return openBlock(), createBlock(_component_gm_upload_pro, mergeProps(props, {
        fileList: fileList.value,
        "onUpdate:fileList": _cache[0] || (_cache[0] = ($event) => fileList.value = $event),
        limit: 1,
        disabled: uploadDisabled.value,
        "on-all-complete": handleComplete,
        "on-remove": handleRemove
      }), createSlots({
        _: 2
        /* DYNAMIC */
      }, [
        unref(slots).default ? {
          name: "default",
          fn: withCtx(() => [
            renderSlot(_ctx.$slots, "default")
          ]),
          key: "0"
        } : {
          name: "file",
          fn: withCtx((slotValue) => [
            renderSlot(_ctx.$slots, "file", normalizeProps(guardReactiveProps(slotValue)))
          ]),
          key: "1"
        },
        unref(slots).trigger ? {
          name: "trigger",
          fn: withCtx(() => [
            renderSlot(_ctx.$slots, "trigger")
          ]),
          key: "2"
        } : void 0,
        unref(slots).tip ? {
          name: "tip",
          fn: withCtx(() => [
            renderSlot(_ctx.$slots, "tip")
          ]),
          key: "3"
        } : void 0,
        unref(slots).fileActions ? {
          name: "fileActions",
          fn: withCtx((slotValue) => [
            renderSlot(_ctx.$slots, "fileActions", normalizeProps(guardReactiveProps(slotValue)))
          ]),
          key: "4"
        } : void 0
      ]), 1040, ["fileList", "disabled"]);
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=UploadItem.vue2.mjs.map
