import{html as e,css as t}from"../../../lit/index.js";import{SimpleFieldsUpload as o}from"../../simple-fields/lib/simple-fields-upload.js";import{winEventsElement as i,localStorageGet as a}from"../../utils/utils.js";import{HAXStore as s}from"./hax-store.js";import{SuperDaemonInstance as l}from"../../super-daemon/super-daemon.js";import{I18NMixin as r}from"../../i18n-manager/lib/I18NMixin.js";class HaxUploadField extends(i(r(o))){constructor(){super(),this.showSources=!0,this.autocomplete="on",this.__winEvents={"hax-app-picker-selection":"_haxAppPickerSelection"},this.t=this.t||{},this.t={...this.t,whereUpload:"Where would you like to upload this",cantHandle:"Sorry, you don't have a storage location that can handle",uploads:"uploads",dropMediaHereOr:"drop media here or",selectMedia:"Select media",upload:"Upload",takePhoto:"Take photo",recordAudio:"Record audio",cancel:"Cancel",uploadMedia:"Upload media"},this.registerLocalization({context:this,namespace:"hax"})}static get tag(){return"hax-upload-field"}_canUpload(){return!this.__allowUpload&&s}static get properties(){return{...super.properties,showSources:{type:Boolean,reflect:!0,attribute:"show-sources"}}}_fileAboutToUpload(e){if(this._canUpload()){e.preventDefault(),e.stopPropagation();let o={source:e.detail.file.name,type:e.detail.file.type};var t=s.guessGizmoType(o);let i=s.getHaxAppStoreTargets(t);1===i.length?this._haxAppPickerSelection({detail:i[0]}):0!==i.length?s.haxAppPicker.presentOptions(i,t,`${this.t.whereUpload} ${t}?`,"app"):s.toast(`${this.t.cantHandle} ${t} ${this.t.uploads}!`,5e3)}else this.__allowUpload=!1}_haxAppPickerSelection(e){let t=e.detail.connection;this.__appUsed=e.detail,this.shadowRoot.querySelector("#fileupload").method=t.operations.add.method;let o=t.protocol+"://"+t.url;"/"!=o.substr(o.length-1)&&(o+="/"),void 0!==t.operations.add.endPoint&&(o+=t.operations.add.endPoint),null!=s.connectionRewrites.appendUploadEndPoint&&(o+="?"+s.connectionRewrites.appendUploadEndPoint),null!=s.connectionRewrites.appendJwt&&(o+="&"+s.connectionRewrites.appendJwt+"="+a(s.connectionRewrites.appendJwt)),this.shadowRoot.querySelector("#fileupload").headers=t.headers,this.shadowRoot.querySelector("#fileupload").target=o,this.__allowUpload=!0,this.shadowRoot.querySelector("#fileupload").uploadFiles()}_fileUploadResponse(e){let t=JSON.parse(e.detail.xhr.response),o=this.__appUsed.connection.operations.add.resultMap,i={},a={};for(var s in void 0!==this._resolveObjectPath(o.item,t)&&(i=this._resolveObjectPath(o.item,t)),a.type=o.defaultGizmoType,o.gizmo)a[s]=this._resolveObjectPath(o.gizmo[s],i);void 0===a.url&&void 0!==a.source&&(a.url=a.source),void 0!==o.gizmo.type&&(a.type=this._resolveObjectPath(o.gizmo.type,i)),this.shadowRoot.querySelector("#url").value=a.url}get sources(){return e` <simple-toolbar-button
        ?disabled="${this.disabled}"
        label="${this.t.selectMedia}.."
        ?show-text-label="${this.responsiveSize.indexOf("s")<0}"
        icon="hax:multimedia"
        show-text-label
        @click="${this._clickMediaButton}"
        controls="fieldset"
        part="merlin"
        ?hidden="${!this.showSources}"
      >
      </simple-toolbar-button>
      ${super.sources}`}_clickMediaButton(e){var t="sources";if(this.label.toLowerCase().includes("image"))t="image";else if(this.label.toLowerCase().includes("video"))t="video";else if(s.haxTray.activeHaxElement){let e=s.guessGizmoType(s.haxTray.activeHaxElement.properties);"*"!=e&&(t=e)}l.runProgram(t),"hax-upload-field"==this.tagName.toLowerCase()&&(l.programTarget=this),l.open()}}customElements.define(HaxUploadField.tag,HaxUploadField);export{HaxUploadField};