import "@johnlindquist/kit"
let { Configuration, OpenAIApi } = await npm("openai")
let configuration = new Configuration({
  apiKey: await env("OPENAI_API_KEY"),
})
Date.prototype.getDateWithDateOrdinal = function () {
    var d = this.getDate();  
    if (d > 3 && d < 21) return d + 'th';
    switch (d % 10) {
        case 1: return d + "st";
        case 2: return d + "nd";
        case 3: return d + "rd";
        default: return d + "th";
    }
}
let tomorrow = new Date()
tomorrow.setDate(new Date().getDate() + 1)
let roamDate = (date) => {
  const monthNames = ["January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
        ]
  return "[[" + monthNames[date.getMonth()] + " " + date.getDateWithDateOrdinal() + ", " + date.getFullYear() + "]]"
}
let openai = new OpenAIApi(configuration)
let complete = async (prompt, maxTokens, temperature) => {
    setTimeout(() => {
        setLoading(true)
    }, 250)
    let response = await openai.createCompletion({
        model: "text-davinci-002",
        prompt: `${prompt}`,
        temperature: temperature,
        max_tokens: maxTokens,
        top_p: 1,
        frequency_penalty: 0,
        presence_penalty: 0,
    })
    setLoading(false)
    return response?.data?.choices[0]?.text?.trim()  
}
let convert = await getSelectedText()
let temperature = 0
let maxTokens = 256
let prompt = `
You try to interpret what I want to say in Tana Paste Format. Put double brackets around dates, people and company names. Today is ${new Date()}.
Replace AF with [[Andre Foeken]]
Replace PB with [[Pieter Bos]]
Convert:
AF uses Tana
Into:
%%tana%%
- [[Andre Foeken]] uses Tana
Convert:
Suggest some fields for a movie
Into:
%%tana%%
- Amazing Movie #movie
  - Title::
  - Release Year::
  - Director::
  - Plot Summary::
Convert:
Add a field Attendees and "Meeting topic"
Into:
%%tana%%
- Add a field Attendees and "Meeting topic"
  - Attendees::
  - Meeting topic::
Convert:
1+1=
Into:
%%tana%%
- 1+1=2
Convert:
Today
Into:
%%tana%%
- ${roamDate(new Date())}
Convert:
Tomorrow
Into:
%%tana%%
- ${roamDate(tomorrow)}
Convert:
3P with John Doe
Into:
%%tana%%
- 3P meeting with [[John Doe]] #3P
  - Attendees:: [[John Doe]]
Convert:
1-1 meeting with John Doe
Into:
%%tana%%
- 1-1 meeting with [[John Doe]] #1-1
  - Attendees:: [[John Doe]]
Convert:
I want to see this today
Into:
I want to see this ${roamDate(new Date())}
Convert:
Try out this thing on Today, Tue, 6 Dec
Into:
%%tana%%
- Try out this thing on [[December 6th, 2022]] #todo
  - Planned:: [[December 6th, 2022]]
Convert:
The Obstacle is the Way
Into:
%%tana%%
- The Obstacle is the Way #book
  - Author:: [[Ryan Holiday]]
Convert:
Ryan Holiday is the author of [[The Obstacle is the Way]]
Into:
%%tana%%
- [[Ryan Holiday]] is the author of [[The Obstacle is the Way]]
Convert:
Ryan Holiday
Into:
%%tana%%
- Ryan Holiday #person
Convert:
Meeting about Compensation Structure with John Ruumpol and Andre Foeken
Into:
%%tana%%
- Meeting about [[Compensation Structure]] with [[John Doe]] and [[Andre Foeken]] #meeting  
  - Attendees::
    - [[John Doe]]
    - [[Andre Foeken]]
Convert: ${convert}
Into:`
let result = await complete(prompt, maxTokens, temperature)
setSelectedText(result)