import "core-js/modules/es.array.iterator.js";
import "core-js/modules/es.function.name.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.string.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import "core-js/modules/web.url.js";
import "core-js/modules/web.url.to-json.js";
import "core-js/modules/web.url-search-params.js";
import "core-js/modules/web.url-search-params.delete.js";
import "core-js/modules/web.url-search-params.has.js";
import "core-js/modules/web.url-search-params.size.js";
import { safelyRemoveChild } from 'agora-foundation/lib/utilities/safely-remove-child';
export function handleClickUploadImage(handleImagData) {
  var fileInput = document.createElement('input');
  fileInput.setAttribute('readonly', 'readonly');
  fileInput.type = 'file';
  fileInput.accept = 'image/*';
  fileInput.style.display = 'none';
  fileInput.addEventListener('change', function (ev) {
    handleFileSelect(ev, handleImagData);
  }, false);
  fileInput.addEventListener('cancel', function () {
    safelyRemoveChild(document.body, fileInput);
  });
  document.body.appendChild(fileInput);
  fileInput.click();
}
function handleFileSelect(event, handleImagData) {
  var _event$target;
  var file = event === null || event === void 0 || (_event$target = event.target) === null || _event$target === void 0 || (_event$target = _event$target.files) === null || _event$target === void 0 ? void 0 : _event$target[0];
  if (file) {
    var reader = new FileReader();
    reader.onload = function (e) {
      var _e$target;
      var fileData = e === null || e === void 0 || (_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.result;
      var url = URL.createObjectURL(file);
      // TODO: 数据类型
      var chatRoomFile = {
        filename: file.name,
        filetype: file.type,
        url: url,
        data: fileData
      };
      handleImagData(chatRoomFile);
      URL.revokeObjectURL(url);
    };
    reader.onerror = function () {
      console.error('文件读取失败');
    };

    // base64 读取
    reader.readAsDataURL(file);
    reader.onloadend = function () {
      event.target.remove();
    };
  }
}