1import{d as J,k as K,f as U,l as z,h as G,m as w,n as H,r as N,b as P,p as W,s as X,c as Y,t as p,j as o,B as j,L as Z,g as I}from"./main-DaCJV-qv.js";import{p as _,C as tt,P as et}from"./composer-skeleton-Da-X3RNJ.js";import"./popover-D0FxLH7G.js";import{a as ot,b as st}from"./composer-BXq8VY2F.js";import"./calendar-CEyeZQu3.js";import"./checkbox-CCdXlQuY.js";import{u as it}from"./layout-context-CGeXJKV8.js";import{c as rt}from"./ai-CKVQze56.js";import{o as at}from"./publishing-MKyoky0h.js";import{A as nt}from"./arrow-left-CcOHDWaz.js";import"./index-DRcuG5rS.js";import"./select-RiTNqKtD.js";import"./index-BdQq_4o_.js";import"./index-BQEMIoSZ.js";import"./index-5YPqZdip.js";import"./index-8Lin4XP7.js";import"./index-CXUJWoUO.js";import"./index-C8n2GY8S.js";import"./dropdown-menu-JcXU4gql.js";import"./index-BknOjS16.js";import"./copy-CmZnj_Dl.js";import"./sparkles-O3ZKXwQr.js";import"./image-9ej_H16c.js";import"./trash-2-DwEGQFDG.js";import"./command-tX-0jQo_.js";import"./tooltip-BE3B3o0F.js";import"./ellipsis-ClOxJOim.js";import"./repeat-2-j4nD94nP.js";import"./heart-BfXb5nl5.js";import"./triangle-alert-ZtTLiS6T.js";import"./media-uploader-Bnb0duuc.js";import"./progress-fCuD3BFy.js";import"./index-A8RhrAvO.js";import"./upload-ByQQwr4t.js";import"./chevrons-up-down-DCM6GgbZ.js";import"./switch-BGsQ8Reg.js";import"./circle-alert-DSqoee56.js";import"./alert-dialog-DV6Mky3F.js";import"./chevron-right-1F03UPIj.js";async function ct(a){const c=await fetch(`${I()}/v1/posts`,{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify(a)});if(!c.ok){const f=await c.json();throw new Error(f.error||"Failed to create post")}return c.json()}async function lt(){const a=await fetch(`${I()}/v1/media`,{credentials:"include"});if(!a.ok)throw new Error("Failed to fetch media");return(await a.json()).data}function Xt(){const a=J(),c=K(),f=U(),b=()=>{window.history.length>1?c.history.back():a({to:"/dashboard"})},C=z(at),{trackPostCreated:k,trackPostScheduled:L,trackPostPublishedIntent:D}=G(),M=w(ot),v=w(st),O=w(_),[d,B]=H(rt),[h]=N.useState(()=>{if(d){const t={content:d.content,headline:d.headline,accountIds:d.accountIds};return B(null),t}return null}),[u]=N.useState(()=>{if(typeof window>"u")return null;const t=sessionStorage.getItem("duplicatePost");if(t)try{const e=JSON.parse(t);return sessionStorage.removeItem("duplicatePost"),e}catch{return sessionStorage.removeItem("duplicatePost"),null}return null}),{data:g=[],isLoading:E}=P(W()),{data:F,isLoading:Q}=P(X()),q=F?.data??[],{data:R=[],isLoading:T}=P({queryKey:["media"],queryFn:lt}),{selectedProfileId:S}=it(),m=S?g.find(t=>t.id===S)??g[0]:g[0],V=m?q.filter(t=>t.profileId===m.id&&t.active):[],$=(t,e,l)=>{const r={};return t.forEach(s=>{const n=(M.get(s)??[]).filter(i=>i.content.trim().length>0).map(i=>({type:i.type,content:i.content,mediaIds:i.mediaIds}));if(n.length>0){const i=v.get(s),A=O.get(s);console.log(`[compose] Building payload for account ${s}`),console.log("[compose] platformMetadataMap.get(accountId):",i),console.log("[compose] platformSettingsMap.get(accountId):",A),r[s]={items:n,...i&&{metadata:i},...A&&{settings:A},...e?.[s]&&{redditSubreddit:e[s]},...l?.[s]&&{redditTitle:l[s]}},console.log("[compose] Final platforms[accountId]:",r[s])}}),r},x=Y({mutationFn:t=>{const e={profileId:m?.id||"",platforms:t.platforms,...t.scheduledAt&&{scheduledAt:t.scheduledAt},...t.publishNow!==void 0&&{publishNow:t.publishNow},...t.autoSchedule!==void 0&&{autoSchedule:t.autoSchedule},...t.status&&{status:t.status}};return ct(e)},onSuccess:async(t,e)=>{if(await f.refetchQueries({queryKey:["posts"]}),k(e.socialAccountIds.length),e.scheduledAt?L(new Date(e.scheduledAt),e.socialAccountIds.length):e.publishNow&&D(e.socialAccountIds.length),e.publishNow){const r=Array.isArray(t.data)?t.data:[t.data],s=r.map(n=>({id:n.socialAccountId,platform:n.platform})),y=r[0]?.id;y&&C(n=>[...n,{postId:y,platforms:s,startedAt:new Date().toISOString()}])}if(!e.publishNow)if(e.status==="draft")p.success("Draft saved");else if(e.autoSchedule)p.success("Post auto-scheduled",{description:"Optimal times selected for each platform"});else if(e.scheduledAt){const r=new Date(e.scheduledAt);
1p.success("Post scheduled",{description:r.toLocaleString("en-US",{weekday:"short",month:"short",day:"numeric",hour:"numeric",minute:"2-digit"})})}else p.success("Draft saved");let l="/dashboard";e.status==="draft"?l="/dashboard/drafts":e.publishNow&&(l="/dashboard/published"),a({to:l})}});return E||Q||T?o.jsx(tt,{}):m?o.jsxs("div",{className:"relative h-full",children:[o.jsxs(j,{variant:"ghost",onClick:b,className:"absolute left-4 top-4",children:[o.jsx(nt,{className:"h-4 w-4"}),"Back"]}),o.jsxs("div",{className:"mx-auto max-w-340 space-y-12 py-20 px-6 flex flex-col",children:[o.jsx("div",{className:"relative",children:o.jsxs("div",{className:"flex flex-col gap-2",children:[o.jsx("h1",{className:"text-3xl font-bold text-foreground",children:"Create Post"}),o.jsx("p",{className:"text-base text-muted-foreground",children:"Publish or schedule posts for one or more social accounts."})]})}),o.jsx(et,{accounts:V,allMedia:R,onSubmit:t=>{const e=$(t.socialAccountIds,t.redditSubreddits,t.redditTitles);x.mutate({...t,platforms:e})},isSubmitting:x.isPending,error:x.error?.message,initialValues:u?{content:u.content,accountIds:u.accountIds,mediaIds:u.mediaIds}:h?{content:h.content,accountIds:h.accountIds}:void 0})]})]}):o.jsx("div",{className:"mx-auto max-w-4xl space-y-6 p-6",children:o.jsxs("div",{className:"text-center",children:[o.jsx("h1",{className:"text-2xl font-bold",children:"No Profile"}),o.jsx("p",{className:"mt-2 text-muted-foreground",children:"Create a profile first to start posting."}),o.jsx(j,{asChild:!0,className:"mt-4",children:o.jsx(Z,{to:"/settings/profiles",children:"Create Profile"})})]})})}export{Xt as component};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.