import e from"@babel/runtime/helpers/defineProperty";import t from"@babel/runtime/helpers/slicedToArray";import{jsxs as r,jsx as n}from"react/jsx-runtime";import{forwardRef as o,useState as i,useRef as a,useMemo as c,useEffect as s,useCallback as f,useImperativeHandle as l}from"react";import{padStart as u}from"lodash";import m from"classnames";import d from"moment";import{Tabs as p}from"../../Tabs/Tabs.js";import b from"./TimePicker.scss.js";function v(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function h(t){for(var r=1;r<arguments.length;r++){var n=null!=arguments[r]?arguments[r]:{};r%2?v(Object(n),!0).forEach((function(r){e(t,r,n[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):v(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}var O=new Array(48).fill(0).map((function(e,t){var r=new Date(2e3,0,1,0,15*t),n=r.getHours(),o=u(String(0===n?12:n),2,"0"),i=u(String(r.getMinutes()),2,"0");return{offset:900*t,title:"".concat(o,":").concat(i)}})),g=[{value:"am",label:"AM"},{value:"pm",label:"PM"}],y=function(e){var t=e.offset,r=e.title,o=e.setOffset,i=e.disabled,a=e.selected,c=f((function(){return o(t)}),[o,t]);return n("button",{"data-offset":t,"data-selected":String(a),className:m(b.button,i&&b.disabled,a&&b.selected),onClick:c,disabled:i,children:r})},j=o((function(e,o){var u=e.value,v=e.onChange,j=!u,P=i(),T=t(P,2),w=T[0],D=T[1],M=a(null),S=a(!1),N=i(),k=t(N,2),x=k[0],A=k[1],C=c((function(){return g.map((function(e){return h(h({},e),{},{disabled:j})}))}),[j]),H=c((function(){if(u){var e=d(u).startOf("day").toDate();return Math.ceil((u.getTime()-e.getTime())/1e3)}}),[u]);s((function(){D(void 0!==H?H>=43200?"pm":"am":void 0)}),[H]);var E=c((function(){return O.map((function(e){return h(h({},e),{},{offset:e.offset+("pm"===w?43200:0)})}))}),[w]);s((function(){setTimeout((function(){if(void 0!==H&&M.current&&!S.current){S.current=!1;var e=M.current.querySelector('[data-selected="true"]');if(e){var t=e.offsetTop,r=e.clientHeight,n=t-(M.current.clientHeight-r)/2;M.current.scrollTo({top:Math.max(n,0)})}}}),0)}),[H]),s((function(){var e;j&&(null===(e=M.current)||void 0===e||e.scrollTo({top:0}))}),[j]);var q=f((function(e){S.current=!0,v(e)}),[v]);return l(o,(function(){return{flash:function(){return A(1+Math.random())}}}),[]),r("div",{className:m(b.root,x&&b.flash),children:[n("div",{className:b.header,children:n(p,{size:"small",type:"secondary",items:C,value:w,onChange:D})}),n("div",{className:b.body,children:n("div",{className:m(b.buttons,j&&b.disabled),ref:M,children:E.map((function(e,t,r){var o=e.offset,i=e.title,a=t===r.length-1?o+900:r[t+1].offset;return n(y,{offset:o,title:i,disabled:j,setOffset:q,selected:void 0!==H&&H>=o&&H<a},o)}))})})]},x)}));export{j as TimePicker};
//# sourceMappingURL=TimePicker.js.map
