(()=>{"use strict";const e=window.wp.element,t=window.wp.url,l=window.wp.data;window.wp.editor;const n=window.wp.i18n,a=window.wp.components,r=window.llms.components,i=window.wp.coreData,o=window.llms.utils;function s(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;const l={post_type:"llms_my_certificate"};return e&&(l.sid=e),(0,t.addQueryArgs)(`${(0,o.getAdminUrl)()}/post-new.php`,l)}const d="llms-award-certificate-wrapper",c={},m={},u=function(){let e=document.querySelector(".page-title-action");return e&&(c.top="-3px",c.position="relative"),e||(e=document.getElementById("llms-new-award-button"),c.marginBottom="20px",c.display="inline-block",m.studentId=parseInt((0,t.getQueryArg)(window.location,"student_id")),m.selectStudent=!1),e&&(m.buttonLabel=e.textContent),e}();(function(){if(!u)return!1;const e=document.createElement("span");return e.id=d,Object.entries(c).forEach((t=>{let[l,n]=t;e.style[l]=n})),u.style.display="none",u.after(e),!0})()&&(0,e.render)((0,e.createElement)((function(d){let{modalTitle:c=(0,n.__)("Award a New Certificate","lifterlms"),buttonLabel:m=(0,n.__)("Award","lifterlms"),isDisabled:u=!1,enableScratch:p=!0,selectStudent:w=!0,selectTemplate:f=!0,studentId:_=null,templateId:y=null}=d;const[h,g]=(0,e.useState)(!1),[b,E]=(0,e.useState)(!1),[C,x]=(0,e.useState)(_),[S,v]=(0,e.useState)(y),A=()=>g(!1),B=C&&S;return(0,e.createElement)(e.Fragment,null,h&&(0,e.createElement)(a.Modal,{title:c,style:{maxWidth:"420px"},onRequestClose:A},(0,e.createElement)("p",null,function(e,t){let l="";return e&&t?l=(0,n.__)("Create a new certificate award from the selected template for the selected student.","lifterlms"):e&&!t?l=(0,n.__)("Create a new certificate award from this template for the selected student.","lifterlms"):!e&&t&&(l=(0,n.__)("Create a new certificate award from the selected template for this student.","lifterlms")),l}(w,f)),w&&(0,e.createElement)(r.UserSearchControl,{isClearable:!0,label:(0,n.__)("Award to","lifterlms"),selectedValue:_?[_]:[],onUpdate:e=>{const t=(null==e?void 0:e.id)||null;x(t)}}),f&&(0,e.createElement)(r.PostSearchControl,{isClearable:!0,postType:"llms_certificate",label:(0,n.__)("Template","lifterlms"),placeholder:(0,n.__)("Search for a certificate template…","lifterlms"),selectedValue:y?[y]:[],onUpdate:e=>{const t=(null==e?void 0:e.id)||null;v(t)}}),(0,e.createElement)("div",{style:{textAlign:"right",padding:"24px 32px 0",margin:"24px -32px 0",borderTop:"1px solid #ddd"}},(0,e.createElement)(a.Button,{style:{marginRight:"5px"},disabled:!B,isBusy:b,variant:"primary",onClick:()=>{E(!0),function(e,t){const{saveEntityRecord:n}=(0,l.dispatch)(i.store);return n("postType","llms_my_certificate",{author:e,certificate_template:t,status:"draft"})}(C,S).then((e=>{let{id:l}=e;var n;window.location=(n=l,(0,t.addQueryArgs)(`${(0,o.getAdminUrl)()}/post.php`,{post:n,action:"edit",newAwardMsg:1}))}))}},(0,n.__)("Create Draft")),p&&(0,e.createElement)(a.Button,{style:{marginRight:"5px"},variant:"secondary",href:s(C)},(0,n.__)("Start from Scratch","lifterlms")),(0,e.createElement)(a.Button,{variant:"tertiary",onClick:A},(0,n.__)("Cancel","lifterlms")))),(0,e.createElement)(a.Button,{disabled:u,variant:"secondary",onClick:()=>g(!0)},m))}),m),document.getElementById(d))})();