"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const K=require("../tslib.es6-C4EgNkz1.js"),n=require("react"),x=require("makeup-expander"),Q=require("classnames"),X=require("../utils-C9NL3q0j.js"),b=require("../calendar-B6lRC_bC.js"),l=require("../textbox-5DvabGMY.js");require("../icon-I30uh1Ov.js");const Z=require("../dropdown-B6wMNyjP.js"),$=require("../icons/ebay-icon-calendar-24/index.js"),{default:ee=x}=x?.default?.__esModule?x.default:x,te=600,ne=y=>{var{className:R,inputPlaceholderText:i="YYYY-MM-DD",a11yOpenPopoverText:N="open calendar",range:o,value:v,rangeEnd:S,defaultValue:q,defaultRangeEnd:D,collapseOnSelect:w,children:M,onChange:p=()=>{},onInputChange:A=()=>{},onInputRangeEndChange:L=()=>{}}=y,j=K.__rest(y,["className","inputPlaceholderText","a11yOpenPopoverText","range","value","rangeEnd","defaultValue","defaultRangeEnd","collapseOnSelect","children","onChange","onInputChange","onInputRangeEndChange"]);const m=n.useRef(null),[V,d]=n.useState(q||""),[B,u]=n.useState(D||""),f=l.isControlled(v)?v:V,T=l.isControlled(S)?S:B,s=b.dateArgToISO(f),c=b.dateArgToISO(T),[C,I]=n.useState(!1),[Y,z]=n.useState(1),E=X.filterByType(M,l.EbayTextbox),_=o?E[1]||n.createElement(l.EbayTextbox,null):E[0]||n.createElement(l.EbayTextbox,null),F=o?E[0]||n.createElement(l.EbayTextbox,null):E[1]||n.createElement(l.EbayTextbox,null),{overlayStyles:H,refs:h}=Z.useFloatingDropdown({open:C}),g=h.host,W=()=>{I(!0)},k=()=>{I(!1)};n.useEffect(()=>{if(!g.current)return;m.current=new ee(g.current,{hostSelector:".ebay-date-textbox--main > .icon-btn",contentSelector:".date-textbox__popover",expandOnClick:!0,autoCollapse:!0}),g.current.addEventListener("expander-expand",W),g.current.addEventListener("expander-collapse",k);const e=()=>{z(document.documentElement.clientWidth<te?1:2)};return e(),window.addEventListener("resize",e),()=>{var t;(t=m.current)===null||t===void 0||t.destroy(),window.removeEventListener("resize",e)}},[]);const O=(e,t)=>{const r=new Date(e.target.value),a=isNaN(r.getTime())?null:b.toISO(r);t===0?d(a||""):u(a||""),o?p(e,{rangeStart:t===0?a:s,rangeEnd:t===1?a:c}):p(e,{selected:a})},U=(e,{iso:t})=>{if(d(t),o){const r=s||c,a={rangeStart:t,rangeEnd:r};s&&c?(u(""),a.rangeEnd=null):r&&(r<t?(d(r),u(t),a.rangeStart=r,a.rangeEnd=t):(d(t),u(r),a.rangeStart=t,a.rangeEnd=r)),p(e,a)}else p(e,{selected:t});w&&(m.current.expanded=!1)},P=(e,t)=>{t===0?(d(e.target.value),A(e)):(u(e.target.value),L(e))},[G,J]=Array.isArray(i)?i:[i,i];return n.createElement("span",{className:Q("date-textbox",R),ref:h.setHost},o&&n.cloneElement(F,{value:f,placeholder:G,onInputChange:e=>P(e,0),onBlur:e=>O(e,0)}),n.cloneElement(_,Object.assign(Object.assign({},_.props),{className:"ebay-date-textbox--main",placeholder:J,value:o?T:f,onInputChange:e=>P(e,o?1:0),onBlur:e=>O(e,o?1:0)}),n.createElement(l.EbayTextboxPostfixIcon,{icon:n.createElement($.EbayIconCalendar24,null),buttonAriaLabel:N})),n.createElement("div",{hidden:!C,ref:h.setOverlay,style:H,className:"date-textbox__popover"},n.createElement(b.EbayCalendar,Object.assign({},j,{range:o,interactive:!0,navigable:!0,numMonths:Y,selected:s&&c?[s,c]:s||c||void 0,onSelect:U}))))};exports.EbayDateTextbox=ne;
