import { useStore } from '@nanostores/solid'
import { currentConversationId, conversationMap } from '@/stores/conversation'
import type { Conversation } from '@/types/conversation'
import { fetchData } from '../../http/api'


export default () => {
  const $currentConversationId = useStore(currentConversationId)
  const $conversationMap = useStore(conversationMap)

  const currentConversation = () => {
    return $conversationMap()[$currentConversationId()]
  }

  const handleShareMessage = async (conversation: Conversation) =>  {
    var conversation = currentConversation();
    fetchData({id:conversation.id, title: conversation.name }, function(data) {
      if(data.code==200){
        // window.location.href = data.url;
        window.open(data.url) 
      }else{
        alert(data.message);
      }
    }, '/chatgptApi/createShare', 'POST');
  }
  
  return (
    <>
      { $currentConversationId() && (
        <div class="fcc p-2 rounded-md text-xl hv-foreground" onClick={() => { handleShareMessage(true) }} >
          <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-3 h-3">
            <path stroke-linecap="round" stroke-linejoin="round" d="M7.217 10.907a2.25 2.25 0 100 2.186m0-2.186c.18.324.283.696.283 1.093s-.103.77-.283 1.093m0-2.186l9.566-5.314m-9.566 7.5l9.566 5.314m0 0a2.25 2.25 0 103.935 2.186 2.25 2.25 0 00-3.935-2.186zm0-12.814a2.25 2.25 0 103.933-2.185 2.25 2.25 0 00-3.933 2.185z" />
          </svg>
        </div>
      )}
    </>
  )
}
