import e from"@babel/runtime/helpers/slicedToArray";import{jsxs as t,Fragment as o,jsx as n}from"react/jsx-runtime";import{useRef as r,useState as i,useMemo as a,useCallback as l}from"react";import u from"moment";import{storeApi as d}from"../../Store/Store.js";import"../../Store/utils.js";import{Tooltip as m}from"../../../Tooltip/Tooltip.js";import{suggestName as s,isOwnTemplate as c}from"../../utils.js";import{ButtonIcon as p}from"../../../ButtonIcon/ButtonIcon.js";import{UnsavedChangesBadge as f}from"../../UnsavedChangesBadge/UnsavedChangesBadge.js";import{IconMenu as v}from"../IconMenu/IconMenu.js";import{TemplatesTreeMenuHeader as T}from"../../TemplatesTreeMenuHeader/TemplatesTreeMenuHeader.js";import{Action as h}from"../../types.js";import g from"./MenuHeader.scss.js";var j=function(){var j,I=d.useTemplateMenuData,b=d.useTemplateById,C=d.useSelectedTemplate,U=d.useHasUnsavedChanges,y=d.useAllTemplates,M=d.useTemplateIcon,B=d.useTemplateIconColor,N=d.useUpdateTemplateIcon,S=d.useHasCurrentAction,E=d.useUpdateTemplateName,H=I(),A=C(),x=U(null==H?void 0:H.templateId),_=y(),k=b(null==H?void 0:H.templateId),q=B(k),w=N(),z=M(k),D=S(null==k?void 0:k.id,h.UpdateMetadata),P=S(null==k?void 0:k.id,h.UpdateName),V=E(),F=r(null),G=i(!1),J=e(G,2),K=J[0],L=J[1],O=a((function(){return _.filter((function(e){return e.id!==(null==k?void 0:k.id)}))}),[_,null==k?void 0:k.id]),Q=l((function(e){var t=0===e.trim().length?"Template must have a name":"",o=t?"":s(e,O),n=o?'Name should be unique. Try "'.concat(o,'"'):t,r=n||function(e){if(null!=e&&e.updated_at){var t=u(e.updated_at).fromNow();return"".concat(e.published?"Published":"Edited"," ").concat(t)}return""}(k);return{message:r,hasError:!!n}}),[O,k]),R=l((function(e){V(null==k?void 0:k.id,e)}),[null==k?void 0:k.id,V]),W=l((function(){return L((function(e){return!e}))}),[]),X=l((function(e){var t=e.icon,o=e.iconColor;W(),w(null==k?void 0:k.id,{icon:t,iconColor:o})}),[null==k?void 0:k.id,W,w]);if(!k)return null;var Y=c(k);return t(o,{children:[n(T,{entityName:null==k?void 0:k.name,onSave:R,badge:k.id===(null==A?void 0:A.id)&&x&&n(f,{className:g.unsavedChanges}),button:n(m,{title:"Edit Icon",disabled:!Y,children:n(p,{size:"small",disabled:!Y||D,type:"tertiaryBg",ref:F,onClick:W,children:n(z,{color:q})})}),isEditable:Y,isUpdating:P,getValidation:Q}),n(v,{open:K,onSave:X,anchorEl:F.current,icon:null===(j=k.metadata)||void 0===j?void 0:j.icon,iconColor:q})]})};export{j as MenuHeader};
//# sourceMappingURL=MenuHeader.js.map
