import e,{Component as t}from"react";import{DomHandler as n,UniqueComponentId as r,classNames as a,ObjectUtils as i}from"primereact/utils";import{Ripple as o}from"primereact/ripple";function l(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n=t}))||r.reverse().find((function(e){return!e.tab.props.disabled&&t>e.index}))}},{key:"onTabHeaderClose",value:function(e,t){var n,r=this,a=[].concat(s(n=this.state.hiddenTabs)||c(n)||u(n)||p(),[t]);this.setState({hiddenTabs:a},(function(){var n=r.findVisibleActiveTab(t);n&&r.onTabHeaderClick(e,n.tab,n.index)})),this.props.onTabClose&&this.props.onTabClose({originalEvent:e,index:t}),e.preventDefault()}},{key:"onTabHeaderClick",value:function(e,t,n){t.props.disabled||(this.props.onTabChange?this.props.onTabChange({originalEvent:e,index:n}):this.setState({activeIndex:n})),this.updateScrollBar(n),e.preventDefault()}},{key:"onKeyDown",value:function(e,t,n){"Enter"===e.code&&this.onTabHeaderClick(e,t,n)}},{key:"updateInkBar",value:function(){var e=this.getActiveIndex(),t=this["tab_".concat(e)];this.inkbar.style.width=n.getWidth(t)+"px",this.inkbar.style.left=n.getOffset(t).left-n.getOffset(this.nav).left+"px"}},{key:"updateScrollBar",value:function(e){var t=this["tab_".concat(e)];t&&t.scrollIntoView({block:"nearest"})}},{key:"updateButtonState",value:function(){var e=this.content,t=e.scrollLeft,r=e.scrollWidth,a=n.getWidth(e);this.setState({backwardIsDisabled:0===t}),this.setState({forwardIsDisabled:t===r-a})}},{key:"onScroll",value:function(e){this.props.scrollable&&this.updateButtonState(),e.preventDefault()}},{key:"getVisibleButtonWidths",value:function(){return[this.prevBtn,this.nextBtn].reduce((function(e,t){return t?e+n.getWidth(t):e}),0)}},{key:"navBackward",value:function(){var e=this.content,t=n.getWidth(e)-this.getVisibleButtonWidths(),r=e.scrollLeft-t;e.scrollLeft=r<=0?0:r}},{key:"navForward",value:function(){var e=this.content,t=n.getWidth(e)-this.getVisibleButtonWidths(),r=e.scrollLeft+t,a=e.scrollWidth-t;e.scrollLeft=r>=a?a:r}},{key:"reset",value:function(){var e={backwardIsDisabled:!0,forwardIsDisabled:!1,hiddenTabs:[]};this.props.onTabChange?this.props.onTabChange({index:this.props.activeIndex}):e=T(T({},e),{},{activeIndex:this.props.activeIndex}),this.setState(e)}},{key:"componentDidMount",value:function(){this.state.id||this.setState({id:r()}),this.updateInkBar()}},{key:"componentDidUpdate",value:function(e){this.updateInkBar(),e.activeIndex!==this.props.activeIndex&&this.updateScrollBar(this.props.activeIndex)}},{key:"renderTabHeader",value:function(t,n){var r=this,l=this.isSelected(n),s=T(T({},t.props.headerStyle||{}),t.props.style||{}),c=a("p-unselectable-text",{"p-tabview-selected p-highlight":l,"p-disabled":t.props.disabled},t.props.headerClassName,t.props.className),u=this.state.id+"_header_"+n,p=this.state.id+"_content_"+n,d=t.props.disabled?null:0,f=t.props.leftIcon&&e.createElement("i",{className:t.props.leftIcon}),b=e.createElement("span",{className:"p-tabview-title"},t.props.header),h=t.props.rightIcon&&e.createElement("i",{className:t.props.rightIcon}),v=e.createElement("a",{role:"tab",className:"p-tabview-nav-link",onClick:function(e){return r.onTabHeaderClick(e,t,n)},id:u,onKeyDown:function(e){return r.onKeyDown(e,t,n)},"aria-controls":p,"aria-selected":l,tabIndex:d},f,b,h,t.props.closable&&e.createElement("i",{className:"p-tabview-close pi pi-times",onClick:function(e){return r.onTabHeaderClose(e,n)}}),e.createElement(o,null));t.props.headerTemplate&&(v=i.getJSXElement(t.props.headerTemplate,{className:"p-tabview-nav-link",titleClassName:"p-tabview-title",onClick:function(e){return r.onTabHeaderClick(e,t,n)},onKeyDown:function(e){return r.onKeyDown(e,t,n)},leftIconElement:f,titleElement:b,rightIconElement:h,element:v,props:this.props,index:n,selected:l,ariaControls:p}));return e.createElement("li",{ref:function(e){return r["tab_".concat(n)]=e},className:c,style:s,role:"presentation"},v)}},{key:"renderTabHeaders",value:function(){var t=this;return e.Children.map(this.props.children,(function(e,n){if(t.shouldTabRender(e,n))return t.renderTabHeader(e,n)}))}},{key:"renderNavigator",value:function(){var t=this,n=this.renderTabHeaders();return e.createElement("div",{ref:function(e){return t.content=e},id:this.props.id,className:"p-tabview-nav-content",style:this.props.style,onScroll:this.onScroll},e.createElement("ul",{ref:function(e){return t.nav=e},className:"p-tabview-nav",role:"tablist"},n,e.createElement("li",{ref:function(e){return t.inkbar=e},className:"p-tabview-ink-bar"})))}},{key:"renderContent",value:function(){var t=this,n=e.Children.map(this.props.children,(function(e,n){if(t.shouldTabRender(e,n)&&(!t.props.renderActiveOnly||t.isSelected(n)))return t.createContent(e,n)}));return e.createElement("div",{className:"p-tabview-panels"},n)}},{key:"createContent",value:function(t,n){var r=this.isSelected(n),i=T(T({},t.props.contentStyle||{}),t.props.style||{}),o=a(t.props.contentClassName,t.props.className,"p-tabview-panel",{"p-hidden":!r});return e.createElement("div",{id:this.state.id+"_content_"+n,"aria-labelledby":this.state.id+"_header_"+n,"aria-hidden":!r,className:o,style:i,role:"tabpanel"},this.props.renderActiveOnly?r&&t.props.children:t.props.children)}},{key:"renderPrevButton",value:function(){var t=this;return this.props.scrollable&&!this.state.backwardIsDisabled?e.createElement("button",{ref:function(e){return t.prevBtn=e},className:"p-tabview-nav-prev p-tabview-nav-btn p-link",onClick:this.navBackward,type:"button"},e.createElement("span",{className:"pi pi-chevron-left"}),e.createElement(o,null)):null}},{key:"renderNextButton",value:function(){var t=this;if(this.props.scrollable&&!this.state.forwardIsDisabled)return e.createElement("button",{ref:function(e){return t.nextBtn=e},className:"p-tabview-nav-next p-tabview-nav-btn p-link",onClick:this.navForward,type:"button"},e.createElement("span",{className:"pi pi-chevron-right"}),e.createElement(o,null))}},{key:"render",value:function(){var t=a("p-tabview p-component",this.props.className,{"p-tabview-scrollable":this.props.scrollable}),n=this.renderNavigator(),r=this.renderContent(),i=this.renderPrevButton(),o=this.renderNextButton();return e.createElement("div",{className:t},e.createElement("div",{className:"p-tabview-nav-container"},i,n,o),r)}}]),v}();g(x,"defaultProps",{id:null,activeIndex:0,style:null,className:null,renderActiveOnly:!0,onTabChange:null,onTabClose:null,scrollable:!1});export{C as TabPanel,x as TabView};