import{__awaiter,__decorate}from"tslib";import{SuperComponent,wxComponent}from"../common/src/index";import config from"../common/config";import props from"./props";const{prefix:prefix}=config,name=`${prefix}-chat-sender`;let ChatSender=class extends SuperComponent{constructor(){super(...arguments),this.options={multipleSlots:!0},this.properties=props,this.data={classPrefix:name,scrollViewTop:0,focusFlag:!1,isSending:!1,inputStyle:"",originalMarginBottom:12,files:[],uploadPlacement:"bottom",uploadConfig:{uploadCamera:{iconClass:"camera",text:"拍摄",handler:"handleImageUpload",handlerArg:"camera"},uploadImage:{iconClass:"image",text:"图片",handler:"handleImageUpload",handlerArg:"album"},uploadAttachment:{iconClass:"file-add",text:"文件",handler:"handleWechatFileUpload",handlerArg:"attachment"}},uploadNames:[]},this.observers={fileList(t){this.setData({files:t?JSON.parse(JSON.stringify(t)):[]})},renderPresets(t){const e=t.find(t=>Array.isArray(t.presets));this.setData({uploadNames:e?e.presets:[]})}},this.methods={onkeyboardheightchange(t){if(this.triggerEvent("keyboardheightchange",t.detail),!this.data.autoRiseWithKeyboard)return;const e=t.detail.height;if(e>0){const t=e+this.data.originalMarginBottom;this.setData({inputStyle:`margin-bottom: ${t}px;`})}else this.setData({inputStyle:""})},handleSendClick(t){this.data.loading?this.handleStop(t):this.sendClick(t)},handleOutsideClick(){this.triggerEvent("updatevisible",!1)},sendClick(t){this.data.value&&!this.data.disabled&&this.triggerEvent("send",{value:this.data.value,e:t})},handleStop(t){this.triggerEvent("stop",{value:this.data.value,e:t},{bubbles:!1})},handlerClick(){this.data.disabled?this.setData({focusFlag:!1}):this.setData({focusFlag:!0})},focusFn(t){this.setData({focusFlag:!0}),this.triggerEvent("focus",{value:t.detail.value,context:t})},blurFn(t){this.setData({focusFlag:!1}),this.triggerEvent("blur",{value:t.detail.value,context:t})},textChange(t){this.setData({value:t.detail.value}),this.triggerEvent("change",{value:t.detail.value,context:t})},handleUploadClick(t){const{status:e}=t.currentTarget.dataset;this.triggerEvent("uploadClick"),this.data.disabled||"disabled"===e||this.triggerEvent("updateVisible",!this.data.visible)},handleFileClick(t){const{item:e}=t.detail;this.triggerEvent("fileClick",{file:e})},handleFileRemove(t){if(!Array.isArray(this.data.files))return;const{item:e,index:i}=t.detail;this.triggerEvent("fileDelete",{file:e});const a=[...this.data.files];a.splice(i,1),this.setData({files:a}),this.triggerEvent("fileChange",{files:a})},handleFileAdd(){this.triggerEvent("fileAdd")},handleImageUpload(t){return __awaiter(this,void 0,void 0,function*(){const{type:e}=t.currentTarget.dataset,i=[e];try{const a=yield wx.chooseImage({count:1,sizeType:["original","compressed"],sourceType:i});if(a.tempFilePaths&&a.tempFilePaths.length>0){const i=a.tempFilePaths.map(t=>({url:t,name:t,size:0,fileType:"image"})),s="album"===e?"uploadImage":"uploadCamera";this.triggerEvent("fileSelect",{e:t,name:s,files:i});const l=[...this.data.files,...i];this.setData({files:l}),this.triggerEvent("fileChange",{files:l})}}catch(t){wx.showToast({title:"album"===e?"选择图片失败":"拍照失败",icon:"none"})}finally{this.triggerEvent("updatevisible",!1)}})},handleWechatFileUpload(t){return __awaiter(this,void 0,void 0,function*(){try{const e=yield wx.chooseMessageFile({count:5,type:"all"});if(e.tempFiles&&e.tempFiles.length>0){const i=e.tempFiles.map(t=>Object.assign(Object.assign({},t),{url:t.path})),a=[...this.data.files,...i];this.setData({files:a}),this.triggerEvent("fileSelect",{e:t,name:"uploadAttachment",files:i}),this.triggerEvent("fileChange",{files:a})}}catch(t){wx.showToast({title:"选择微信文件失败",icon:"none"})}finally{this.triggerEvent("updatevisible",!1)}})},handleUploadEntryClick(t){const{name:e}=t.currentTarget.dataset,i=this.data.uploadConfig[e];i&&this[i.handler]&&this[i.handler]({currentTarget:{dataset:{type:i.handlerArg}}})}},this.lifetimes={created(){this.data.onkeyboardheightchange=this.onkeyboardheightchange.bind(this),this.data.handleSendClick=this.handleSendClick.bind(this),this.data.handleOutsideClick=this.handleOutsideClick.bind(this),this.data.sendClick=this.sendClick.bind(this),this.data.handleStop=this.handleStop.bind(this),this.data.handlerClick=this.handlerClick.bind(this),this.data.focusFn=this.focusFn.bind(this),this.data.blurFn=this.blurFn.bind(this),this.data.textChange=this.textChange.bind(this),this.data.handleUploadClick=this.handleUploadClick.bind(this),this.data.handleFileClick=this.handleFileClick.bind(this),this.data.handleFileRemove=this.handleFileRemove.bind(this),this.data.handleFileAdd=this.handleFileAdd.bind(this),this.data.handleImageUpload=this.handleImageUpload.bind(this),this.data.handleWechatFileUpload=this.handleWechatFileUpload.bind(this),this.data.handleUploadEntryClick=this.handleUploadEntryClick.bind(this)},attached(){},detached(){}}}};ChatSender=__decorate([wxComponent()],ChatSender);export default ChatSender;