var __decorate=this&&this.__decorate||function(t,e,i,o){var r,n=arguments.length,s=n<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,i):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,i,o);else for(var l=t.length-1;l>=0;l--)(r=t[l])&&(s=(n<3?r(s):n>3?r(e,i,s):r(e,i))||s);return n>3&&s&&Object.defineProperty(e,i,s),s};import{InputType as t}from"@openremote/or-mwc-components/or-mwc-input";import{css as e,html as i,LitElement as o}from"lit";import{customElement as r,property as n,state as s}from"lit/decorators.js";import l from"moment";import{buttonStyle as a}from"../style";import{OrRulesJsonRuleChangedEvent as d}from"./or-rule-json-viewer";import{TimeTriggerType as c}from"../index";import{Util as p}from"@openremote/core";import{OrMwcDialogOpenedEvent as u}from"@openremote/or-mwc-components/or-mwc-dialog";import{OrMapClickedEvent as g}from"@openremote/or-map";import{i18next as h}from"@openremote/or-translate";let style=e`
    
    ${a}
    
    :host {
        display: block;
    }
    
    .trigger-group {
        flex-grow: 1;
        display: flex;
        align-items: center;
        flex-direction: row;
        flex-wrap: wrap;
    }
    .min-width {
        min-width: 200px;
    }
    .width {
        width: 200px;
    }
    
    .trigger-group > * {
        margin: 10px 3px 6px 3px;
    }
`,OrRuleTriggerQuery=class extends o{static get styles(){return style}constructor(){super(),this.readonly=!1,this.triggerOptions=[],Object.values(c).forEach(t=>{this.triggerOptions.push({key:t,value:this.triggerToString(t)})}),this.getSunPositions().forEach(t=>{this.triggerOptions.push({key:t,value:this.triggerToString(t)})}),this.selectedTrigger=this.triggerOptions[0],this.addEventListener(u.NAME,this.initMap)}updated(t){t.has("condition")&&(this.condition.cron?this.selectedTrigger={key:c.TIME_OF_DAY,value:this.triggerToString(c.TIME_OF_DAY)}:this.condition.sun&&(this.selectedTrigger={key:this.condition.sun.position,value:this.triggerToString(this.condition.sun.position)}))}initMap(){let t=this.shadowRoot.getElementById("map-modal");if(!t)return;let e=t.shadowRoot.querySelector(".or-map");e&&e.addEventListener(g.NAME,e=>{let i=e.detail.lngLat;this.setLocation({type:"Point",coordinates:[i.lat,i.lng]});let o=t.shadowRoot.querySelector(".location-lat"),r=t.shadowRoot.querySelector(".location-lng");o.value=i.lat,r.value=i.lng})}renderDialogHTML(t){let e=this.shadowRoot.getElementById("map-modal");e&&(e.content=i`
                <div style="display:grid">
                    <or-map class="or-map" type="VECTOR" style="border: 1px solid #d5d5d5; height: 400px; min-width: 300px; margin-bottom: 20px;">
                        ${t&&t.coordinates?i`
                            <or-map-marker class="or-map-marker" active color="#FF0000" icon="white-balance-sunny" lat="${t.coordinates[0]}" lng="${t.coordinates[1]}"></or-map-marker>
                        `:void 0}
                    </or-map>
                    <div class="layout horizontal">
                        <input hidden class="location-lng"  required placeholder=" " type="text" .value="${t&&t.coordinates?t.coordinates[0]:null}" />
                        <input hidden class="location-lat" required placeholder=" " type="text" .value="${t&&t.coordinates?t.coordinates[1]:null}" />
                    </div>
                </div>
            `)}render(){var e,o;let r=[{actionName:"close",default:!0,content:i`<or-mwc-input class="button" .type="${t.BUTTON}" label="close"></or-mwc-input>`,action:()=>{}}],n=()=>{var t;let e=this.shadowRoot.getElementById("map-modal");e&&(e.dismissAction=null,e.open(),this.renderDialogHTML(null==(t=this.condition.sun)?void 0:t.location))};return this.renderDialogHTML(null==(e=this.condition.sun)?void 0:e.location),i`
            <div class="trigger-group">
                <or-mwc-input class="min-width" type="${t.SELECT}" .options="${this.triggerOptions.map(t=>t.value)}" .value="${this.selectedTrigger.value}" label="${h.t("triggerType")}"
                              @or-mwc-input-changed="${t=>{this.setTrigger(this.triggerOptions.find(e=>e.value==t.detail.value))}}">
                </or-mwc-input>
                ${this.selectedTrigger?i`
                    ${this.selectedTrigger.key==this.triggerOptions[0].key?i`
                        <or-mwc-input class="min-width" type="${t.TIME}" .value="${this.condition.cron?l(p.cronStringToISOString(this.condition.cron,!0)).format("HH:mm"):void 0}" label="${h.t("timeOfDay")}"
                                      @or-mwc-input-changed="${t=>{this.setTime(t.detail.value)}}">
                        </or-mwc-input>
                    `:i`
                        <or-mwc-input class="min-width width" type="${t.NUMBER}" .value="${null==(o=this.condition.sun)?void 0:o.offsetMins}" label="${h.t("offsetInMinutes")}"
                                      @or-mwc-input-changed="${t=>{this.setOffset(t.detail.value)}}">
                        </or-mwc-input>
                        <or-mwc-input type="${t.BUTTON}" class="min-width" @or-mwc-input-changed="${n}" label="location"></or-mwc-input>
                        <or-mwc-dialog id="map-modal" heading="${h.t("pickLocation")}" .actions="${r}"></or-mwc-dialog>
                    `}
                `:void 0}
            </div>
        `}setTrigger(t){t&&(t.key==c.TIME_OF_DAY?this.condition.sun=void 0:this.getSunPositions().includes(t.key)&&(this.condition.cron=void 0,this.getSunPositions().includes(this.selectedTrigger.key)?this.condition.sun={position:t.key,offsetMins:this.condition.sun.offsetMins,location:this.condition.sun.location}:this.condition.sun={position:t.key,offsetMins:0}),this.selectedTrigger=t,this.dispatchEvent(new d),this.requestUpdate())}setTime(t){if(t){let e=t.split(":"),i=new Date;i.setHours(Number(e[0])),i.setMinutes(Number(e[1])),this.condition.cron=p.formatCronString(void 0,void 0,void 0,i.getUTCHours().toString(),i.getUTCMinutes().toString()),this.dispatchEvent(new d),this.requestUpdate()}}setOffset(t){var e,i;this.condition.sun={position:null==(e=this.condition.sun)?void 0:e.position,location:null==(i=this.condition.sun)?void 0:i.location,offsetMins:t},this.dispatchEvent(new d),this.requestUpdate()}setLocation(t){var e,i;this.condition.sun={position:null==(e=this.condition.sun)?void 0:e.position,location:t,offsetMins:null==(i=this.condition.sun)?void 0:i.offsetMins},this.dispatchEvent(new d),this.requestUpdate()}getSunPositions(){return["TWILIGHT_MORNING_CIVIL","SUNRISE","SUNSET","TWILIGHT_EVENING_CIVIL"]}triggerToString(t){return t==c.TIME_OF_DAY?h.t("timeOfDay"):h.t(t.toLowerCase())}};__decorate([n({type:Object,attribute:!1})],OrRuleTriggerQuery.prototype,"condition",void 0),__decorate([n()],OrRuleTriggerQuery.prototype,"readonly",void 0),__decorate([s()],OrRuleTriggerQuery.prototype,"selectedTrigger",void 0),__decorate([s()],OrRuleTriggerQuery.prototype,"triggerOptions",void 0),OrRuleTriggerQuery=__decorate([r("or-rule-trigger-query")],OrRuleTriggerQuery);export{OrRuleTriggerQuery};