import { defineComponent, ref, computed, openBlock, createBlock, unref, withCtx, createElementBlock, normalizeClass, createElementVNode, createCommentVNode } from "vue";
import TUIChatEngine, { TUIStore, StoreName, TUIChatService } from "@tencentcloud/chat-uikit-engine";
import { TUIGlobal } from "@tencentcloud/universal-api";
import "../../../../adapter-vue.mjs";
import { isUniFrameWork, isPC, isWeChat } from "../../../../utils/env.mjs";
import ToolbarItemContainer from "../toolbar-item-container/index.vue.mjs";
import imageIconLight from "../../../../assets/icon/image-light.svg.mjs";
import imageIconDark from "../../../../assets/icon/image-dark.svg.mjs";
import imageUniIcon from "../../../../assets/icon/image-uni.png.mjs";
import cameraUniIcon from "../../../../assets/icon/camera-uni.png.mjs";
import { isEnabledMessageReadReceiptGlobal } from "../../utils/utils.mjs";
import OfflinePushInfoManager from "../../offlinePushInfoManager/index.mjs";
import ChatConfig from "../../config.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    // Image source: only valid for uni-app version, web version only supports selecting images from the album.
    // album: Select from album
    // camera: Take a photo using the camera
    imageSourceType: {
      type: String,
      default: "album"
    }
  },
  setup(__props) {
    const props = __props;
    const inputRef = ref();
    const currentConversation = ref();
    const theme = ChatConfig.getTheme();
    const IMAGE_TOOLBAR_SHOW_MAP = {
      web_album: {
        icon: theme === "dark" ? imageIconDark : imageIconLight,
        title: "图片"
      },
      uni_album: {
        icon: imageUniIcon,
        title: "图片"
      },
      uni_camera: {
        icon: cameraUniIcon,
        title: "拍照"
      }
    };
    TUIStore.watch(StoreName.CONV, {
      currentConversation: (conversation) => {
        currentConversation.value = conversation;
      }
    });
    const imageToolbarForShow = computed(() => {
      if (isUniFrameWork) {
        return props.imageSourceType === "camera" ? IMAGE_TOOLBAR_SHOW_MAP["uni_camera"] : IMAGE_TOOLBAR_SHOW_MAP["uni_album"];
      } else {
        return IMAGE_TOOLBAR_SHOW_MAP["web_album"];
      }
    });
    const onIconClick = () => {
      var _a, _b, _c, _d;
      if (isUniFrameWork) {
        if (isWeChat && ((_a = TUIGlobal) == null ? void 0 : _a.chooseMedia)) {
          (_b = TUIGlobal) == null ? void 0 : _b.chooseMedia({
            count: 1,
            mediaType: ["image"],
            sizeType: ["original", "compressed"],
            sourceType: [props.imageSourceType],
            // Use camera or select from album.
            success: function(res) {
              sendImageMessage(res);
            }
          });
        } else {
          (_c = TUIGlobal) == null ? void 0 : _c.chooseImage({
            count: 1,
            sourceType: [props.imageSourceType],
            // Use camera or select from album.
            success: function(res) {
              sendImageMessage(res);
            }
          });
        }
      } else {
        if ((_d = inputRef.value) == null ? void 0 : _d.click) {
          inputRef.value.click();
        }
      }
    };
    const sendImageInWeb = (e) => {
      var _a, _b;
      if (((_b = (_a = e == null ? void 0 : e.target) == null ? void 0 : _a.files) == null ? void 0 : _b.length) <= 0) {
        return;
      }
      sendImageMessage(e == null ? void 0 : e.target);
      e.target.value = "";
    };
    const sendImageMessage = (files) => {
      var _a, _b, _c, _d, _e;
      if (!files) {
        return;
      }
      const options = {
        to: ((_b = (_a = currentConversation == null ? void 0 : currentConversation.value) == null ? void 0 : _a.groupProfile) == null ? void 0 : _b.groupID) || ((_d = (_c = currentConversation == null ? void 0 : currentConversation.value) == null ? void 0 : _c.userProfile) == null ? void 0 : _d.userID),
        conversationType: (_e = currentConversation == null ? void 0 : currentConversation.value) == null ? void 0 : _e.type,
        payload: {
          file: files
        },
        needReadReceipt: isEnabledMessageReadReceiptGlobal()
      };
      const offlinePushInfoCreateParams = {
        conversation: currentConversation.value,
        payload: options.payload,
        messageType: TUIChatEngine.TYPES.MSG_IMAGE
      };
      const sendMessageOptions = {
        offlinePushInfo: OfflinePushInfoManager.create(offlinePushInfoCreateParams)
      };
      TUIChatService.sendImageMessage(options, sendMessageOptions);
    };
    return (_ctx, _cache) => {
      return openBlock(), createBlock(ToolbarItemContainer, {
        iconFile: unref(imageToolbarForShow).icon,
        title: unref(imageToolbarForShow).title,
        iconWidth: unref(isUniFrameWork) ? "32px" : "20px",
        iconHeight: unref(isUniFrameWork) ? "25px" : "18px",
        needDialog: false,
        onOnIconClick: onIconClick
      }, {
        default: withCtx(() => [
          !unref(isUniFrameWork) ? (openBlock(), createElementBlock("div", {
            key: 0,
            class: normalizeClass(["image-upload", !unref(isPC) && "image-upload-h5"])
          }, [
            createElementVNode("input", {
              ref_key: "inputRef",
              ref: inputRef,
              title: "图片",
              type: "file",
              "data-type": "image",
              accept: "image/gif,image/jpeg,image/jpg,image/png,image/bmp,image/webp",
              onChange: sendImageInWeb
            }, null, 544)
          ], 2)) : createCommentVNode("", true)
        ]),
        _: 1
      }, 8, ["iconFile", "title", "iconWidth", "iconHeight"]);
    };
  }
});
export {
  _sfc_main as default
};
