import type { CmsContentEditorState, CmsPostContent } from "@roottale/cms-client/server";

/**
 * 공개 화면과 미리보기의 사이트 문서 변환에서 같은 함수를 사용합니다.
 * API의 content_editor: null은 SDK에서 실패 결과가 되므로 기본 상태로 대체하지 않습니다.
 * 결과가 없는 경우에만 저장된 편집 상태가 없는 것으로 처리합니다.
 */
export function contentEditorState(post: CmsPostContent): CmsContentEditorState {
  const result = post.contentEditorResult;
  if (result) {
    if (!result.success) throw new Error(result.error);
    return result.data;
  }
  return { sectionStates: {}, primaryTermId: null, cta: { mode: "auto", targetPostId: null } };
}
