import Taro, { useRouter } from "@tarojs/taro";
import { useState } from "react";
import { useAgentStore } from "@/store/agentStore";
import { TAgentDetail } from "@/types/agent";

const useEditAgent = (
  editKey: string,
  initValue?: string,
  _agent?: TAgentDetail
) => {
  const router = useRouter();
  const { agentId } = router.params;
  const agent = useAgentStore((state) => _agent ?? state.agent);
  const agentCharacter = useAgentStore((state) => state.agentCharacter);
  const [value, setValue] = useState(initValue ?? "");
  const { editAgentCharacter,fetchAgent } = useAgentStore();
  const handleSubmit = async () => {
    if (!agent?.agentId) {
      return;
    }
    if (value.length <= 0) {
      return;
    }
    if(!agentCharacter){
      return
    }

    // {
    //   enabledPersonalKb: agent.enabledPersonalKb ?? false,
    //   greeting: agent.greeting ?? '',
    //   personality: value,
    //   questionGuides: agent.questionGuides ?? [],
    //   voiceId: agent.voiceId ?? '',
    // }
    const result = await editAgentCharacter(agent.agentId, {
      ...agentCharacter,
      [editKey]: value,
    });
    if(result){
      await fetchAgent(agent.agentId)
      Taro.navigateBack()
    }
  };
  const onChange = (e: any) => {
    setValue(e);
  };
  return {
    value,
    setValue,
    onChange,
    handleSubmit,
  };
};

export default useEditAgent;
