import { defineComponent, shallowRef, onMounted, watch, watchEffect, triggerRef, createVNode, computed, TransitionGroup, withDirectives, vShow } from 'vue';
import { PictureTwoTone, FileTwoTone, LoadingOutlined, PaperClipOutlined } from '@ant-design/icons-vue';
import '../../../../utils/index.mjs';
import '../../../../vue-components/index.mjs';
import { previewImage, isImageUrl } from '../utils.mjs';
import { uploadListProps } from '../interface.mjs';
import { Button } from '../../../button/index.mjs';
import useConfigInject from '../../../config-provider/src/hooks/useConfigInject.mjs';
import ListItem from './ListItem.mjs';
import { filterEmpty } from '../../../../utils/vue/props.mjs';
import { initDefaultProps } from '../../../../utils/vue/initDefaultProps.mjs';
import { isValidElement } from '../../../../utils/is.mjs';
import { collapseMotion } from '../../../../vue-components/transition/src/collapseMotion.mjs';
import { getTransitionGroupProps } from '../../../../vue-components/transition/src/transition.mjs';

function HackSlot(_, {
  slots
}) {
  var _a;
  return filterEmpty((_a = slots.default) == null ? void 0 : _a.call(slots))[0];
}
var UploadList = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AUploadList",
  props: initDefaultProps(uploadListProps(), {
    listType: "text",
    // or picture
    progress: {
      strokeWidth: 2,
      showInfo: false
    },
    showRemoveIcon: true,
    showDownloadIcon: false,
    showPreviewIcon: true,
    previewFile: previewImage,
    isImageUrl,
    items: [],
    appendActionVisible: true
  }),
  setup(props, {
    slots,
    expose
  }) {
    const motionAppear = shallowRef(false);
    onMounted(() => {
      motionAppear.value === true;
    });
    const mergedItems = shallowRef([]);
    watch(() => props.items, (val = []) => {
      mergedItems.value = val.slice();
    }, {
      immediate: true,
      deep: true
    });
    watchEffect(() => {
      if (props.listType !== "picture" && props.listType !== "picture-card")
        return;
      let hasUpdate = false;
      (props.items || []).forEach((file, index) => {
        if (typeof document === "undefined" || typeof window === "undefined" || !window.FileReader || !window.File || !(file.originFileObj instanceof File || file.originFileObj instanceof Blob) || file.thumbUrl !== void 0)
          return;
        file.thumbUrl = "";
        if (props.previewFile) {
          props.previewFile(file.originFileObj).then((previewDataUrl) => {
            const thumbUrl = previewDataUrl || "";
            if (thumbUrl !== file.thumbUrl) {
              mergedItems.value[index].thumbUrl = thumbUrl;
              hasUpdate = true;
            }
          });
        }
      });
      if (hasUpdate)
        triggerRef(mergedItems);
    });
    const onInternalPreview = (file, e) => {
      if (!props.onPreview)
        return;
      e == null ? void 0 : e.preventDefault();
      return props.onPreview(file);
    };
    const onInternalDownload = (file) => {
      if (typeof props.onDownload === "function")
        props.onDownload(file);
      else if (file.url)
        window.open(file.url);
    };
    const onInternalClose = (file) => {
      var _a;
      (_a = props.onRemove) == null ? void 0 : _a.call(props, file);
    };
    const internalIconRender = ({
      file
    }) => {
      const iconRender = props.iconRender || slots.iconRender;
      if (iconRender)
        return iconRender({
          file,
          listType: props.listType
        });
      const isLoading = file.status === "uploading";
      const fileIcon = props.isImageUrl && props.isImageUrl(file) ? createVNode(PictureTwoTone, null, null) : createVNode(FileTwoTone, null, null);
      let icon = isLoading ? createVNode(LoadingOutlined, null, null) : createVNode(PaperClipOutlined, null, null);
      if (props.listType === "picture")
        icon = isLoading ? createVNode(LoadingOutlined, null, null) : fileIcon;
      else if (props.listType === "picture-card")
        icon = isLoading ? props.locale.uploading : fileIcon;
      return icon;
    };
    const actionIconRender = (opt) => {
      const {
        customIcon,
        callback,
        prefixCls: prefixCls2,
        title
      } = opt;
      const btnProps = {
        type: "text",
        size: "small",
        title,
        onClick: () => {
          callback();
        },
        class: `${prefixCls2}-list-item-action`
      };
      if (isValidElement(customIcon))
        return createVNode(Button, btnProps, {
          icon: () => customIcon
        });
      return createVNode(Button, btnProps, {
        default: () => [createVNode("span", null, [customIcon])]
      });
    };
    expose({
      handlePreview: onInternalPreview,
      handleDownload: onInternalDownload
    });
    const {
      prefixCls,
      rootPrefixCls
    } = useConfigInject("upload", props);
    const listClassNames = computed(() => ({
      [`${prefixCls.value}-list`]: true,
      [`${prefixCls.value}-list-${props.listType}`]: true
    }));
    const transitionGroupProps = computed(() => {
      const motion = {
        ...collapseMotion(`${rootPrefixCls.value}-motion-collapse`)
      };
      delete motion.onAfterAppear;
      delete motion.onAfterEnter;
      delete motion.onAfterLeave;
      const motionConfig = {
        ...getTransitionGroupProps(`${prefixCls.value}-${props.listType === "picture-card" ? "animate-inline" : "animate"}`),
        class: listClassNames.value,
        appear: motionAppear.value
      };
      return props.listType !== "picture-card" ? {
        ...motion,
        ...motionConfig
      } : motionConfig;
    });
    return () => {
      const {
        listType,
        locale,
        isImageUrl: isImgUrl,
        showPreviewIcon,
        showRemoveIcon,
        showDownloadIcon,
        removeIcon,
        previewIcon,
        downloadIcon,
        progress,
        appendAction,
        itemRender,
        appendActionVisible
      } = props;
      const appendActionDom = appendAction == null ? void 0 : appendAction();
      const items = mergedItems.value;
      return createVNode(TransitionGroup, {
        ...transitionGroupProps.value,
        "tag": "div"
      }, {
        default: () => [items.map((file) => {
          const {
            uid: key
          } = file;
          return createVNode(ListItem, {
            "key": key,
            "locale": locale,
            "prefixCls": prefixCls.value,
            "file": file,
            "items": items,
            "progress": progress,
            "listType": listType,
            "isImgUrl": isImgUrl,
            "showPreviewIcon": showPreviewIcon,
            "showRemoveIcon": showRemoveIcon,
            "showDownloadIcon": showDownloadIcon,
            "onPreview": onInternalPreview,
            "onDownload": onInternalDownload,
            "onClose": onInternalClose,
            "removeIcon": removeIcon,
            "previewIcon": previewIcon,
            "downloadIcon": downloadIcon,
            "itemRender": itemRender
          }, {
            ...slots,
            iconRender: internalIconRender,
            actionIconRender
          });
        }), appendAction ? withDirectives(createVNode(HackSlot, {
          "key": "__ant_upload_appendAction"
        }, {
          default: () => appendActionDom
        }), [[vShow, !!appendActionVisible]]) : null]
      });
    };
  }
});

export { UploadList as default };
//# sourceMappingURL=index.mjs.map
