"use client"

import { useParams, useSearchParams } from "next/navigation"
import { useState } from "react"
import { PublicOfficeEditor } from "@/components/drive/public-office-editor"
import { filePathFromPublicEditSegments } from "@/lib/drive/public-share-url"

export default function PublicShareEditPage() {
  const params = useParams()
  const searchParams = useSearchParams()
  const token = String(params.token ?? "")
  const pathSegments = params.path as string[] | undefined
  const filePath = filePathFromPublicEditSegments(token, pathSegments)
  const returnTo = searchParams.get("returnTo")
  const mode = searchParams.get("mode") === "view" ? "view" : "edit"
  const fileDisplayName = searchParams.get("name") ?? undefined
  const [password] = useState<string | undefined>(() => {
    if (typeof window === "undefined") return undefined
    return sessionStorage.getItem(`public-share-pw:${token}`) ?? undefined
  })

  return (
    <PublicOfficeEditor
      token={token}
      filePath={filePath}
      password={password}
      returnTo={returnTo}
      mode={mode}
      fileDisplayName={fileDisplayName}
    />
  )
}
