define(["require","exports","tslib","react","date-fns","react-date-range","../assets/arrow","../assets/datetime","../common","../FormElementOuter","../../BrowserUtils","../dateUtilities","../utils","../styles/daterange","../constants"],function(e,t,A,k,x,U,V,B,T,O,P,q,R,a,j){"use strict";var I;Object.defineProperty(t,"__esModule",{value:!0}),k=A.__importStar(k),O=A.__importDefault(O),R.addStyleToDocument(a.CSS),(a=I=I||{}).Start="start",a.End="end";var K,z=q.getLocaleDateFormat();t.default=function(e){var t=e.component,a=e.element,n=a.value,r=k.useState(!1),o=r[0],l=r[1],i=k.useState(),s=i[0],d=i[1],u=k.useState(q.getDateAsLocaleFormattedString(null==n?void 0:n.startDate)),c=u[0],f=u[1],m=k.useState(),D=m[0],v=m[1],p=k.useState(q.getDateAsLocaleFormattedString(null==n?void 0:n.endDate)),g=p[0],S=p[1],E=!t.enabled,e=t.props,y=e.host,b=e.name,r=y.renderText(a.tooltip),i=t.enabled?"inner":"inner disabled",u=[a.type,R.makeSafeClassName(b)],m=!P.isIE();k.useEffect(function(){A.__awaiter(void 0,void 0,void 0,function(){var t;return A.__generator(this,function(e){switch(e.label){case 0:return"inv"===(t=y.deriveLocale())||"en"===t||"en-US"===t?[2]:[4,q.getDateFnsLocale(t)];case 1:return K=e.sent(),[2]}})})},[]);function C(e,t){var a,n=""!==(l="string"==typeof e?e:e.target.value)&&void 0!==l,r=n?q.tryParseDateText(l):void 0,o=n?r?x.parse(r,z,0):new Date(NaN):void 0,l=(e=t===I.Start)?o:s,t=e?D:o;n?q.isValidDate(o)?e?(d(l),f(r),(a=!q.isValidDate(t)||x.isAfter(l,t))&&(v(l),S(r)),F(),w(l,a?l:t)):(v(t),S(r),(a=!q.isValidDate(l)||x.isBefore(t,l))&&(d(t),f(r)),F(),w(a?t:l,t)):(F({status:"invalid"}),w(),(e?d:v)(new Date(NaN))):((e?void 0===t||q.isValidDate(t):void 0===l||q.isValidDate(l))&&F(),w(),(e?d:v)(void 0))}function N(e,t){(t===I.Start?f:S)(e.target.value)}function h(e,t){var a;e.stopPropagation(),"Enter"===e.key?(a=t===I.Start?c:g,C(a,t)):"Escape"===e.key&&(e=t===I.Start?q.getDateAsLocaleFormattedString(s):q.getDateAsLocaleFormattedString(D),C(e,t))}function w(e,t){_(e,t)&&L(e,t)}function F(e){a.error=e,t.forceUpdate()}var p=k.useCallback(function(e){T.renderFocus(e,b,y)},[]),_=function(e,t){return!(e||t||!n)||(!(!e||!t||n)||!!(e&&t&&n)&&(!x.isEqual(e,n.startDate)||!x.isEqual(t,n.endDate)))},L=function(e,t){a.value=e&&t?{refValueType:"daterange",startDate:e,endDate:t}:void 0,y.post("changed",{name:b,value:a.value})},e={startDate:(null==n?void 0:n.startDate)||new Date,endDate:(null==n?void 0:n.endDate)||new Date,key:"selection",showDateDisplay:!1};return k.default.createElement(O.default,{className:u.join(" "),component:t,element:a},k.default.createElement("div",{className:"outer",title:r},k.default.createElement("div",{className:i},k.default.createElement("input",{ref:p,disabled:E,type:"text",onChange:function(e){return N(e,I.Start)},onBlur:function(e){return C(e,I.Start)},onKeyDown:function(e){return h(e,I.Start)},placeholder:z.toUpperCase(),value:c}),k.default.createElement("span",{className:"arrow"},V.renderArrowRight()),k.default.createElement("input",{disabled:E,type:"text",onChange:function(e){return N(e,I.End)},onBlur:function(e){return C(e,I.End)},onKeyDown:function(e){return h(e,I.End)},placeholder:z.toUpperCase(),value:g})),k.default.createElement("div",{className:"buttonContainer"},m&&k.default.createElement("button",{className:"calendarButton",disabled:E,onClick:function(){l(!o)}},B.renderCalendar()),o&&k.default.createElement("div",{className:j.CssClasses.DROPDOWN},k.default.createElement(U.DateRange,{dateDisplayFormat:z,editableDateInputs:!1,months:1,direction:"horizontal",ranges:[e],onChange:function(e){var t=e.selection.startDate,e=e.selection.endDate;d(t),f(q.getDateAsLocaleFormattedString(t)),v(e),S(q.getDateAsLocaleFormattedString(e)),F(),w(t,e)},locale:K}),k.default.createElement("button",{className:"done",onClick:function(){l(!1)}},y.renderText("@common-done"))))))}});