import{__decorate as t}from"tslib";import{html as i,nothing as e}from"lit";import{property as s,state as n,customElement as a}from"lit/decorators.js";import{s as l}from"./nile-calendar.css.esm.js";import{classMap as r}from"lit/directives/class-map.js";import{w as h}from"../internal/watch.esm.js";import{N as o}from"../internal/nile-element.esm.js";import{C as d}from"./nile-calendar.enum.esm.js";import"../internal/accessibility/a11y.state.enum.esm.js";import"../internal/accessibility/a11y.property.enum.esm.js";import"../internal/accessibility/role.enum.esm.js";let c=class extends o{constructor(){super(...arguments),this.valueAttribute=null,this.allowedDates=JSON.stringify({}),this.range=!1,this.hideTypes=!1,this.doubleClickUnselect=!1,this.allowClear=!1,this.type="absolute",this.hideDurationFields=[],this.showManualInputs=!1,this.showMonthDropdown=!1,this.showYearDropdown=!1,this.dateFormat="MM/DD/YYYY",this.startDate=null,this.endDate=null,this.isSelectingStart=!0,this.hideInput=!1,this.currentMonth=(new Date).getMonth(),this.currentYear=(new Date).getFullYear(),this.allowedDatesLocal=null,this.disabled=!1}static get styles(){return[l]}connectedCallback(){super.connectedCallback(),this.initializeValue(),this.emit("nile-init")}disconnectedCallback(){super.disconnectedCallback(),this.emit("nile-destroy")}updated(t){if(super.updated(t),t.has("valueAttribute")){this.valueAttribute||(this.value=null,this.startDate=null,this.endDate=null,this.isSelectingStart=!0);const t=new Date(this.valueAttribute||"");if(!isNaN(t.getTime())){const i=t.getTimezoneOffset();this.value=new Date(t.getTime()-60*i*1e3),this.currentMonth=this.value.getMonth(),this.currentYear=this.value.getFullYear()}this.initializeValue()}}get monthNames(){return Array.from({length:12},((t,i)=>new Date(0,i).toLocaleString("default",{month:"long"})))}get yearOptions(){const t=this.startYear??2e3,i=this.endYear??2050;return t>i?[]:Array.from({length:i-t+1},((i,e)=>t+e))}onMonthSelected(t){this.currentMonth=t,this.emit("nile-month-change",{month:t})}onYearSelected(t){this.currentYear=t,this.emit("nile-year-change",{year:t})}checkValidAllowedDate(){let t;try{t=JSON.parse(this.allowedDates)}catch(i){t=null}if(!t||0==Object.keys(t).length)return void(this.allowedDatesLocal=null);this.hideInput=!0;this.getUTCDate(t.startDate)>this.getUTCDate(t.endDate)?console.error("StartDate must be greater than endDate"):this.allowedDatesLocal=t}isPrevDisabled(){return 0===this.currentMonth&&void 0!==this.startYear&&this.currentYear<=this.startYear}isNextDisabled(){return 11===this.currentMonth&&void 0!==this.endYear&&this.currentYear>=this.endYear}formatDate(t){if(!t)return"";const i=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),s=String(t.getDate()).padStart(2,"0");let n=this.dateFormat;return n=n.replace(/YYYY/g,String(i)),n=n.replace(/MM/g,e),n=n.replace(/DD/g,s),n=n.replace(/YY/g,String(i).slice(-2)),n}parseDate(t,i){if(!t||!this.dateFormat)return null;const e=this.dateFormat.replace(/YYYY/,"(?<year>\\d{4})").replace(/YY/,"(?<year>\\d{2})").replace(/MM/,"(?<month>\\d{1,2})").replace(/DD/,"(?<day>\\d{1,2})"),s=new RegExp(`^${e}$`),n=t.match(s);if(!n||!n.groups)return null;const a=2===n.groups.year.length?Number(`20${n.groups.year}`):Number(n.groups.year),l=Number(n.groups.month)-1,r=Number(n.groups.day),h=new Date(a,l,r);return i?h.setHours(i.getHours(),i.getMinutes(),i.getSeconds()):h.setHours(0,0,0),h}handleStartDateInput(t){t.stopPropagation();const i=this.parseDate(t.detail.value,this.startDate);i&&(i.setHours(0,0,0),this.startDate=i,this.currentMonth=i.getMonth(),this.currentYear=i.getFullYear(),this.requestUpdate())}handleEndDateInput(t){t.stopPropagation();const i=this.parseDate(t.detail.value,this.endDate);i&&(i.setHours(23,59,59),this.endDate=i,this.currentMonth=i.getMonth(),this.currentYear=i.getFullYear(),this.requestUpdate())}render(){return i`
      <div
        class=${r({base:!0,base__range:this.range})}
        part="calendar-container"
      >
        <div class=${r({"calendar-config":!0,hidden:!this.range||this.range&&this.hideTypes})}>
          <div class="calendar-switcher" part="calendar-switcher">
            <nile-tab-group centered @nile-tab-show="${this.onTypeChange}" value="${this.type}">
              <nile-tab slot="nav" panel="absolute" tabindex="${this.disabled?"-1":"0"}">Absolute</nile-tab>
              <nile-tab slot="nav" panel="relative" tabindex="${this.disabled?"-1":"0"}">Relative</nile-tab>
            </nile-tab-group>
          </div>
        </div>
 
        ${"relative"==this.type?this.renderRelativeCalendar():""}
        ${"absolute"==this.type?this.renderAbsoluteCalendar():""}

        ${this.range?i`
        <div class="button-container" part="button-container">
          ${this.allowClear?i`
            <nile-button 
            class="clear-button"
            variant="secondary"
            role="button"
            ?disabled="${!this.startDate||!this.endDate||this.disabled}"
            @keydown="${t=>{"Enter"!==t.key&&" "!==t.key||this.clearDate()}}"
            @click="${this.clearDate}"

            > Reset</nile-button>`:e}
          <nile-button 
          class="apply-button"  
          ?disabled="${!this.startDate||!this.endDate||this.disabled}"  
          @click="${this.confimRange}"
          @keydown="${t=>{"Enter"!==t.key&&" "!==t.key||this.confimRange()}}"
          > Apply</nile-button>
        </div>
        `:""}
      </div>
    `}renderAbsoluteCalendar(){return i`
    <div class="calendar-wrapper" part="calendar-wrapper">
      <div class="calendar-container" >
        ${this.renderMonth(this.currentYear,this.currentMonth,this.getDaysArray(this.currentYear,this.currentMonth))}
      </div>
    </div>
    ${this.range?i`
    <div class="calender-input ${this.showManualInputs?"":"hidden"}" >
     <div class="from">
  <div class="from-date">
    <nile-input
      ?disabled="${this.disabled}"
      label="From"
      class="manual-input date-input"
      placeholder="${this.dateFormat}"
      value="${this.formatDate(this.startDate)}"
      @nile-change="${this.handleStartDateInput}"
    ></nile-input>
  </div>

  <div class="from-time">
    <nile-input
      ?disabled="${this.disabled}"
      class="manual-input time-input"
      placeholder="HH:MM:SS"
      value="${this.formatTime(this.startDate)}"
      @nile-change="${this.handleStartTimeInput}"
    ></nile-input>
  </div>
</div>


    <div class="from">
        <div class="from-date">
      <nile-input
      ?disabled="${this.disabled}"
        label="To"
        class="manual-input date-input"
        placeholder="${this.dateFormat}"
        value="${this.formatDate(this.endDate)}"
        @nile-change="${this.handleEndDateInput}"
      ></nile-input>
    </div>
      <div class="from-time">
      <nile-input
      ?disabled="${this.disabled}"
        class="manual-input time-input"
        placeholder="HH:MM:SS"
        value="${this.formatTime(this.endDate)}"
        @nile-change="${this.handleEndTimeInput}"
      ></nile-input>
    </div>
    </div>
    </div>
    `:""}
    `}renderRelativeCalendar(){return i`
      <div class="units-wrapper">
        <div class="unit-container">
  
          ${this.hideDurationFields?.includes("minutes")?"":i`
          <div class="unit-group">
            <div class="duration-name">Minutes</div>
            <div class="duration-units">
              ${this.renderTimeValues("minutes",[1,5,15,30,45])}
            </div>
          </div>
          `}
  
          ${this.hideDurationFields?.includes("hours")?"":i`
          <div class="unit-group">
            <div class="duration-name">Hours</div>
            <div class="duration-units">
              ${this.renderTimeValues("hours",[1,2,3,6,8,12])}
            </div>
          </div>
          `}
  
          ${this.hideDurationFields?.includes("days")?"":i`
          <div class="unit-group">
            <div class="duration-name">Days</div>
            <div class="duration-units">
              ${this.renderTimeValues("days",[1,2,3,4,5,6])}
            </div>
          </div>
          `}
  
          ${this.hideDurationFields?.includes("weeks")?"":i`
          <div class="unit-group">
            <div class="duration-name">Weeks</div>
            <div class="duration-units">
              ${this.renderTimeValues("weeks",[1,2,4,6])}
            </div>
          </div>
          `}
  
          ${this.hideDurationFields?.includes("months")?"":i`
          <div class="unit-group">
            <div class="duration-name">Months</div>
            <div class="duration-units">
              ${this.renderTimeValues("months",[3,6,12,15])}
            </div>
          </div>
          `}
        </div>
      </div>
  
      ${this.range?i`
      <div>
        <div class="unit-input-container">
          <nile-input 
            class="manual-input duration-input" 
            ?disabled="${this.disabled}"
            label="Duration"
            inputmode="numeric"
            type="number"
            value="${this.selectedValue}" 
            @nile-change="${this.handleDurationChange}"
            placeholder="Enter Value"
          ></nile-input>
  
          <nile-select 
            class="manual-input time-input"  
            ?disabled="${this.disabled}"
            label="Unit of time" 
            value="${this.selectedUnit}"
            @nile-change="${this.handleUnitChange}"
          >
            <nile-option value="minutes" class="${this.hideDurationFields?.includes("minutes")?"hidden":""}">Minutes</nile-option>
            <nile-option value="hours" class="${this.hideDurationFields?.includes("hours")?"hidden":""}">Hours</nile-option>
            <nile-option value="days" class="${this.hideDurationFields?.includes("days")?"hidden":""}">Days</nile-option>
            <nile-option value="weeks" class="${this.hideDurationFields?.includes("weeks")?"hidden":""}">Weeks</nile-option>
            <nile-option value="months" class="${this.hideDurationFields?.includes("months")?"hidden":""}">Months</nile-option>
          </nile-select>
        </div>
      </div>
      `:""}
    `}renderTimeValues(t,e){return e.map((e=>i`
          <div
            class="duration__value ${this.selectedUnit===t&&this.selectedValue===e?"duration__value--selected":""}"
            @click=${i=>this.handleTimeValueClick(t,e,i)}
            @keydown="${i=>{"Enter"!==i.key&&" "!==i.key||this.handleTimeValueClick(t,e,i)}}"
          >${e}
          </div>`))}renderMonth(t,s,n){const a=new Date(t,s,1).getDay(),l=new Date(t,s+1,0).getDay(),h=this.getDaysArray(0===s?t-1:t,0===s?11:s-1),o=this.getDaysArray(11===s?t+1:t,11===s?0:s+1),d=h.slice(h.length-a),c=o.slice(0,6-l),u=[...d,...n,...c],v=(t,i,e,s)=>{if(!s)return"";if(!this.range&&this.value){if(t===this.value.getDate()&&i===this.value.getMonth()&&e===this.value.getFullYear())return"selected-date"}const n=this.startDate&&t===this.startDate.getDate()&&i===this.startDate.getMonth()&&e===this.startDate.getFullYear(),a=this.endDate&&t===this.endDate.getDate()&&i===this.endDate.getMonth()&&e===this.endDate.getFullYear();return n?"range-start":a?"range-end":""},p=(t,i,e,s)=>{if(!s)return!1;if(this.startDate&&this.endDate){const s=new Date(e,i,t);return s>=this.startDate&&s<=this.endDate}return!1};return i`
      <div class="calendar">
        <div class="calendar-header">
          <nile-icon
            class="calendar-header__month-navigation"
            name="var(--nile-icon-arrow-left, var(--ng-icon-chevron-left))"
            method="var(--nile-svg-method-fill, var(--ng-svg-method-stroke))"
            color=${this.disabled?"var(--nile-colors-dark-500, var(--ng-colors-fg-disabled))":"black"}
            role="button"
            tabindex="${this.disabled||this.isPrevDisabled()?"-1":"0"}"
            @keydown="${t=>{"Enter"!==t.key&&" "!==t.key||this.prevMonth()}}"
            @click="${this.prevMonth}"
          >
          </nile-icon>
          <div class="calendar-header__center">

${this.showMonthDropdown||this.showYearDropdown?i`
  ${this.showMonthDropdown?i`
    <nile-dropdown ?disabled="${this.disabled}" class="month-dropdown">
      <span
        slot="trigger"
        class="calendar-header__trigger month-trigger"
        role="button"
        part="trigger"
      >
        ${new Date(t,s).toLocaleString("default",{month:"long"})}
        <nile-icon name="arrowdropdown" color=${this.disabled?"var(--nile-colors-dark-500, var(--ng-colors-fg-disabled))":"black"} class="calendar-header__caret"></nile-icon>
      </span>
      <nile-menu>
        <div class="month-scroll-wrapper">
          ${this.monthNames.map(((t,e)=>i`
            <nile-menu-item
              class="month-menu-item"
              ?selected="${e===s}"
              @click="${()=>this.onMonthSelected(e)}"
            >
              ${t}
            </nile-menu-item>
          `))}
        </div>
      </nile-menu>
    </nile-dropdown>
  `:i`
    <span
      >${new Date(t,s).toLocaleString("default",{month:"long"})}</span
    >
  `}
  ${this.showYearDropdown?i`
    <nile-dropdown ?disabled="${this.disabled}" class="year-dropdown">
      <span
        slot="trigger"
        class="calendar-header__trigger"
        role="button"
        part="trigger"
      >
        ${t}
        <nile-icon name="arrowdropdown" color=${this.disabled?"var(--nile-colors-dark-500, var(--ng-colors-fg-disabled))":"black"} class="calendar-header__caret"></nile-icon>
      </span>
      <nile-menu>
        <div class="year-scroll-wrapper">
          ${this.yearOptions.map((e=>i`
              <nile-menu-item
                class="year-menu-item"
                ?selected="${e===t}"
                @click="${()=>this.onYearSelected(e)}"
              >
                ${e}
              </nile-menu-item>
            `))}
        </div>
      </nile-menu>
    </nile-dropdown>
  `:i`
    <span>${t}</span>
  `}
`:i`
  <span
    >${new Date(t,s).toLocaleString("default",{month:"long"})}
    ${t}</span
  >
`}
</div>

          <nile-icon
            class="calendar-header__month-navigation"
            name="var(--nile-icon-arrow-right, var(--ng-icon-chevron-right))"
            method="var(--nile-svg-method-fill, var(--ng-svg-method-stroke))"
            color=${this.disabled?"var(--nile-colors-dark-500, var(--ng-colors-fg-disabled))":"black"}
            role="button"
            tabindex="${this.disabled||this.isNextDisabled()?"-1":"0"}"
            @keydown="${t=>{"Enter"!==t.key&&" "!==t.key||this.nextMonth()}}"
            @click="${this.nextMonth}"
            
          >
          </nile-icon>
        </div>
   
        <div class="days_container">
        ${["Sun","Mon","Tue","Wed","Thu","Fri","Sat"].map((t=>i`<div class="day_name">${t}</div>`))}
          ${u.map(((a,l)=>{const h=l>=d.length&&l<d.length+n.length,o=v(a,s,t,h),c={day_date:!0,"not-allowed":!this.isAllowedDate(a,s,t),"in-range":p(a,s,t,h),filler:!h};return o&&(c[o]=!0),i` 
            <div
              part="calendar-day-${a}"
              class=${r(c)}
             tabindex="${h&&this.isAllowedDate(a,s,t)&&!this.disabled?"0":"-1"}"
              @keydown="${i=>{"Enter"!==i.key&&" "!==i.key||this.selectDate(a,s,t)}}"
              @click="${()=>{h&&this.selectDate(a,s,t)}}"
            >
              <span style="position:relative;">
                ${a}
                ${((t,i,e)=>{const s=new Date;return t===s.getDate()&&i+1===s.getMonth()+1&&e===s.getFullYear()})(a,s,t)&&h?i`<div class="current__date__dot" part="current-date-dot"></div>`:e}
              </span>
            </div>`}))}
        </div>
      </div>
    `}isAllowedDate(t,i,e){if(!this.allowedDatesLocal)return!0;const s=new Date(Date.UTC(e,i,t)),n=this.getUTCDate(this.allowedDatesLocal.startDate),a=this.getUTCDate(this.allowedDatesLocal.endDate);a.setUTCHours(23,59,59,999);return s>=n&&s<=a}clearDate(t){(t??(this.range?"relative"===this.type?d.Relative:d.Range:d.Single))===d.Single?this.value=null:(this.valueAttribute="",this.startDate=null,this.endDate=null,this.selectedUnit=void 0,this.selectedValue=void 0),this.requestUpdate(),this.emit("nile-clear")}selectDate(t,i,e){if(this.disabled)return;const s=new Date(e,i,t);if(this.range)if(this.startDate&&this.endDate&&(this.startDate=null,this.endDate=null),this.isSelectingStart)this.startDate=s,this.endDate&&s>this.endDate&&(this.endDate=null),this.isSelectingStart=!1;else if(this.isSelectingStart=!0,this.startDate&&s<this.startDate)this.startDate=s,this.endDate=null,this.isSelectingStart=!1;else{const t=s;t.setHours(23,59,59,999),this.endDate=t}else{if(String(this.value)==String(s)&&this.doubleClickUnselect)return void this.clearDate();this.value=s,this.emitChangedData({value:this.value})}}initializeValue(){if(this.range)try{const t=JSON.parse(this.valueAttribute||"");this.startDate=new Date(t.startDate),this.endDate=new Date(t.endDate),this.startDate=new Date(this.startDate.getTime()),this.endDate=new Date(this.endDate.getTime()),this.value=null}catch(t){}else if(this.valueAttribute){let t=new Date(this.valueAttribute);t=new Date(t.getTime()-6e4*t.getTimezoneOffset()),isNaN(t.getTime())||(this.value=t,this.currentMonth=this.value.getMonth(),this.currentYear=this.value.getFullYear())}}handleDurationChange(t){t.stopPropagation(),t.detail.value=t.detail.value.replace(/e/gi,"");const i=Number(t.detail.value);i&&(this.selectedValue=i,this.selectedUnit&&this.selectedValue&&this.handleTimeValueClick(this.selectedUnit,this.selectedValue,t))}handleUnitChange(t){t.stopPropagation(),this.selectedUnit=t.detail.value,this.selectedUnit&&this.selectedValue&&this.handleTimeValueClick(this.selectedUnit,this.selectedValue,t)}handleTimeValueClick(t,i,e){this.createRelativePeriod(t,i),this.selectedUnit=t,this.selectedValue=i}createRelativePeriod(t,i){const e=new Date,s=new Date;switch(t){case"minutes":s.setMinutes(s.getMinutes()-i);break;case"hours":s.setHours(s.getHours()-i);break;case"days":s.setDate(s.getDate()-i);break;case"weeks":s.setDate(s.getDate()-7*i);break;case"months":s.setMonth(s.getMonth()-i)}return this.startDate=new Date(s.getTime()),this.endDate=new Date(e.getTime()),{startDate:this.startDate,endDate:this.endDate}}handleStartTimeInput(t){if(t.stopPropagation(),!this.startDate)return void(this.startDate=null);const i=this.parseTime(t.detail.value,this.startDate);i?this.startDate=i:this.startDate.setHours(0,0,0),this.requestUpdate()}handleEndTimeInput(t){if(t.stopPropagation(),!this.endDate)return void(this.endDate=null);const i=this.parseTime(t.detail.value,this.endDate);i?this.endDate=i:this.endDate.setHours(0,0,0),this.requestUpdate()}parseTime(t,i){if(!this.isValidTimeInput(t))return null;const[e,s,n]=t.split(":").map(Number),a=new Date(i.getTime());return a.setHours(e,s,n),a}isValidTimeInput(t){return/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/.test(t)}formatTime(t){if(!t)return"";return`${String(t.getHours()).padStart(2,"0")}:${String(t.getMinutes()).padStart(2,"0")}:${String(t.getSeconds()).padStart(2,"0")}`}nextMonth(){if(!this.disabled)if(11===this.currentMonth){const t=this.currentYear+1;if(void 0!==this.endYear&&t>this.endYear)return;this.currentMonth=0,this.currentYear=t}else this.currentMonth++}prevMonth(){if(!this.disabled)if(0===this.currentMonth){const t=this.currentYear-1;if(void 0!==this.startYear&&t<this.startYear)return;this.currentMonth=11,this.currentYear=t}else this.currentMonth--}confimRange(){this.startDate&&this.endDate&&this.emitChangedData({startDate:this.startDate,endDate:this.endDate})}onTypeChange(t){this.disabled||(this.type=t.detail.value,this.emit("nile-type-change",{value:this.type}))}getUTCDate(t){return new Date(Date.UTC(t.slice(0,4),t.slice(5,7)-1,t.slice(8,10)))}emitChangedData(t){this.emit("nile-changed",t),this.emit("nile-change",t)}getDaysArray(t,i){const e=new Date(t,i+1,0).getDate();return Array.from({length:e},((t,i)=>i+1))}};t([s({type:String,attribute:"value"})],c.prototype,"valueAttribute",void 0),t([s({type:String,attribute:!0,reflect:!0})],c.prototype,"allowedDates",void 0),t([s({type:Boolean,reflect:!0})],c.prototype,"range",void 0),t([s({type:Boolean,attribute:!0,reflect:!0})],c.prototype,"hideTypes",void 0),t([s({type:Boolean,reflect:!0})],c.prototype,"doubleClickUnselect",void 0),t([s({type:Boolean,reflect:!0})],c.prototype,"allowClear",void 0),t([s({type:String,reflect:!0})],c.prototype,"type",void 0),t([s({type:String})],c.prototype,"selectedUnit",void 0),t([s({type:Number})],c.prototype,"selectedValue",void 0),t([s({type:Array,attribute:"hide-duration-fields"})],c.prototype,"hideDurationFields",void 0),t([s({type:Boolean})],c.prototype,"showManualInputs",void 0),t([s({type:Number,attribute:!0,reflect:!0})],c.prototype,"startYear",void 0),t([s({type:Number,attribute:!0,reflect:!0})],c.prototype,"endYear",void 0),t([s({type:Boolean,attribute:!0,reflect:!0})],c.prototype,"showMonthDropdown",void 0),t([s({type:Boolean,attribute:!0,reflect:!0})],c.prototype,"showYearDropdown",void 0),t([s({type:String,reflect:!0,attribute:!0})],c.prototype,"dateFormat",void 0),t([n()],c.prototype,"startDate",void 0),t([n()],c.prototype,"endDate",void 0),t([n()],c.prototype,"isSelectingStart",void 0),t([n()],c.prototype,"hideInput",void 0),t([n()],c.prototype,"value",void 0),t([n()],c.prototype,"currentMonth",void 0),t([n()],c.prototype,"currentYear",void 0),t([n()],c.prototype,"allowedDatesLocal",void 0),t([s({type:Boolean,attribute:!0,reflect:!0})],c.prototype,"disabled",void 0),t([h("allowedDates")],c.prototype,"checkValidAllowedDate",null),c=t([a("nile-calendar")],c);export{c as N};
