import{j as a,r as f,h as D,F as p,a4 as v,k as P}from"./vendor.e6eaca24.js";import{d as E,b as r,R as n,m as h,a as u,u as R,c as S,t as C}from"./index.c6944236.js";import{u as x}from"./useRecaptcha.217097ff.js";import{DiscussionEditor as k}from"./DiscussionViewPage.74b71c4f.js";import{a as z}from"./DiscussionsPage.625952ea.js";import{v as _}from"./v4.90f1a8cc.js";import"./react-textarea-autosize.browser.esm.96b11ef7.js";import"./useDialog.e491a615.js";import"./useAsyncCallbackPending.bf7bbf90.js";import"./utils.4a7ed962.js";import"./UserLink.eec3c14e.js";import"./UserAvatar.acd4ab4e.js";import"./lodash.default.91c04a21.js";import"./MarkdownContent.1e1566a9.js";import"./CodeBox.16e9a888.js";import"./PseudoLink.be9af782.js";import"./onEnterPress.b56e5f51.js";import"./LazyPermissionManager.4e5d6942.js";import"./TimeAgo.c4d8c6b5.js";import"./index.6ed2c2c6.js";import"./UserSearch.2e1d12f5.js";import"./PreviewSearch.7b29c032.js";import"./Pagination.3d9a4d43.js";import"./utils.47c10adb.js";const B="_editor_1ka2f_1";var T={editor:B};let d=e=>{var w;const s=R("discussion_edit"),i=S();f.exports.useEffect(()=>{r.enterNewPage(e.discussion?`${s(".title_update")} #${e.discussion.meta.id}`:s(".title_new"))},[r.locale,e.discussion]);const c=x(),[t,y]=f.exports.useState(e.discussion?e.discussion.meta.title:""),[l,I]=f.exports.useState(e.discussion?e.discussion.content:"");async function $(){var b,g;const{requestError:o,response:m}=e.discussion?await u.discussion.updateDiscussion({discussionId:e.discussion.meta.id,title:t,content:l}):await u.discussion.createDiscussion({problemId:(g=(b=e.problem)==null?void 0:b.meta)==null?void 0:g.id,title:t,content:l},c("CreateDiscussion"));if(o)C.error(o(s));else if(m.error)C.error(s(`.errors.${m.error}`));else{const N=e.discussion?e.discussion.meta.id:m.discussionId;return()=>i.navigate(`/d/${N}`)}return!1}return D(p,{children:[z(e.discussion?e.discussion.problem:e.problem,s,null,D(p,{children:[a(v.Divider,{icon:"right angle"}),a(v.Section,{children:e.discussion?`${s(".header.update")} #${e.discussion.meta.id}`:s(".header.add")})]})),a(k,{className:T.editor,type:e.discussion?"UpdateDiscussion":"NewDiscussion",publisher:((w=e.discussion)==null?void 0:w.publisher)||r.currentUser,title:t,content:l,onChangeTitle:o=>o.length<=80&&y(o),onChangeContent:I,onCancel:()=>i.navigate(`/d/${e.discussion.meta.id}`),onSubmit:$,noSubmitPermission:e.discussion&&!e.discussion.permissions.includes("Modify")})]})};d=P(d);var ne={new:E(async e=>{if(!(r.currentUser&&r.serverPreference.security.allowEveryoneCreateDiscussion||r.currentUserHasPrivilege("ManageDiscussion")))throw new n(h("discussion_edit.errors.PERMISSION_DENIED"));const s=await(async()=>{const i=e.params.problemId;if(i==null)return null;const{requestError:c,response:t}=await u.problem.getProblem({id:Number(i)||0,localizedContentsOfLocale:r.locale,localizedContentsTitleOnly:!0});if(c)throw new n(c,{showRefresh:!0,showBack:!0});if(t.error)throw new n(h(`discussion_edit.errors.${t.error}`));return{meta:t.meta,title:t.localizedContentsOfLocale.title}})();return a(d,{problem:s},_())}),edit:E(async e=>{const{requestError:s,response:i}=await u.discussion.getDiscussionAndReplies({locale:r.locale,discussionId:Number(e.params.id)||0,getDiscussion:!0});if(s)throw new n(s,{showRefresh:!0,showBack:!0});if(i.error)throw new n(h(`discussion_edit.errors.${i.error}`));return a(d,{discussion:i.discussion},_())})};export{ne as default};
