"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const error = require("../../../utils/error.js");
const upload = require("./upload.js");
const index = require("../../../../node_modules/@vueuse/core/index.js");
const vue = require("vue");
const isNil = require("../../../../../lodash-es/isNil.js");
const SCOPE = "ElUpload";
const revokeFileObjectURL = (file) => {
  var _a;
  if ((_a = file.url) == null ? void 0 : _a.startsWith("blob:")) URL.revokeObjectURL(file.url);
};
const useHandlers = (props, uploadRef) => {
  const uploadFiles = index.useVModel(props, "fileList", void 0, { passive: true });
  const getFile = (rawFile) => uploadFiles.value.find((file) => file.uid === rawFile.uid);
  function abort(file) {
    var _a;
    (_a = uploadRef.value) == null ? void 0 : _a.abort(file);
  }
  function clearFiles(states = [
    "ready",
    "uploading",
    "success",
    "fail"
  ]) {
    uploadFiles.value = uploadFiles.value.filter((row) => !states.includes(row.status));
  }
  function removeFile(file) {
    uploadFiles.value = uploadFiles.value.filter((uploadFile) => uploadFile.uid !== file.uid);
  }
  const emitChange = (file) => {
    vue.nextTick(() => props.onChange(file, uploadFiles.value));
  };
  const handleError = (err, rawFile) => {
    const file = getFile(rawFile);
    if (!file) return;
    console.error(err);
    file.status = "fail";
    removeFile(file);
    props.onError(err, file, uploadFiles.value);
    emitChange(file);
  };
  const handleProgress = (evt, rawFile) => {
    const file = getFile(rawFile);
    if (!file) return;
    props.onProgress(evt, file, uploadFiles.value);
    file.status = "uploading";
    file.percentage = Math.round(evt.percent);
  };
  const handleSuccess = (response, rawFile) => {
    const file = getFile(rawFile);
    if (!file) return;
    file.status = "success";
    file.response = response;
    props.onSuccess(response, file, uploadFiles.value);
    emitChange(file);
  };
  const handleStart = (file) => {
    if (isNil.default(file.uid)) file.uid = upload.genFileId();
    const uploadFile = {
      name: file.name,
      percentage: 0,
      status: "ready",
      size: file.size,
      raw: file,
      uid: file.uid
    };
    if (props.listType === "picture-card" || props.listType === "picture") try {
      uploadFile.url = URL.createObjectURL(file);
    } catch (err) {
      error.debugWarn(SCOPE, err.message);
      props.onError(err, uploadFile, uploadFiles.value);
    }
    uploadFiles.value = [...uploadFiles.value, uploadFile];
    emitChange(uploadFile);
  };
  const handleRemove = async (file) => {
    const uploadFile = file instanceof File ? getFile(file) : file;
    if (!uploadFile) error.throwError(SCOPE, "file to be removed not found");
    const doRemove = (file2) => {
      abort(file2);
      removeFile(file2);
      props.onRemove(file2, uploadFiles.value);
      revokeFileObjectURL(file2);
    };
    if (props.beforeRemove) {
      if (await props.beforeRemove(uploadFile, uploadFiles.value) !== false) doRemove(uploadFile);
    } else doRemove(uploadFile);
  };
  function submit() {
    uploadFiles.value.filter(({ status }) => status === "ready").forEach(({ raw }) => {
      var _a;
      return raw && ((_a = uploadRef.value) == null ? void 0 : _a.upload(raw));
    });
  }
  vue.watch(() => props.listType, (val) => {
    if (val !== "picture-card" && val !== "picture") return;
    uploadFiles.value = uploadFiles.value.map((file) => {
      const { raw, url } = file;
      if (!url && raw) try {
        file.url = URL.createObjectURL(raw);
      } catch (err) {
        props.onError(err, file, uploadFiles.value);
      }
      return file;
    });
  });
  vue.watch(uploadFiles, (files) => {
    for (const file of files) {
      file.uid || (file.uid = upload.genFileId());
      file.status || (file.status = "success");
    }
  }, {
    immediate: true,
    deep: true
  });
  return {
    uploadFiles,
    abort,
    clearFiles,
    handleError,
    handleProgress,
    handleStart,
    handleSuccess,
    handleRemove,
    submit,
    revokeFileObjectURL
  };
};
exports.useHandlers = useHandlers;
