import "@johnlindquist/kit";
const smallArg = (placeholder: string) => arg({
    placeholder: placeholder,
    height: 100,
    width: 500
});
const substring = await smallArg("Substring to search:");
const lines = await smallArg("# surrounding lines in results:");
const PROJECT_ROOT = "/Users/jwisskirchen/IdeaProjects";
const CLOSE_LABEL = "Close";
const results = await $`cd ${PROJECT_ROOT} ; find [^z]* -name *.java -exec grep -q ${substring} {} ';' -exec echo "******{}******" ';' -exec grep -${lines} ${substring} {} ';'`;
const tokens = results.toString().replaceAll('<', '<').split('******');
const templates = [];
const files: string[] = [];
for (let i = 0; i < tokens.length - 1; i += 2) {
    files.push(`${tokens[i + 1]}`);
    
    templates.push(`<h3>${tokens[i + 1]}</h3>
        <pre>${tokens[i + 2]}</pre><hr>`
        .replaceAll(`${substring}`,
            `<span class="text-red-500">${substring}</span>`)
    );
}
await div({
    html: templates.join('<br><br>\n'),
    width: 1200,
    height: 700
}, `bg-white text-black text-sm p-2`);
await copy(substring);
const items = files.map(path => ({
    name: path,
    
    display: path.slice(0, path.indexOf('/') + 1) + '..' + path.slice(path.lastIndexOf('/'), path.length)
}));
items.push({ name: CLOSE_LABEL, display: CLOSE_LABEL });
const buttons = `
  <div class="grid grid-col w-screen h-screen justify-around items-center">
      <label class="rounded px-10 py-1 bg-white text-black">Open project in IDEA</label>
      <button
      class="rounded px-10 py-1 bg-black bg-opacity-70 hover:bg-opacity-50"
      v-for="(item, index) in items" :key="item.name" :data-name="item.name" :data-index="index">{{ item.display }}</button>
  </div>  
  `;
let w = await widget(buttons, {
    backgroundColor: '#CCCCAA',
    x: 600,
    y: Math.max(0, 500 - items.length * 25),
    width: 600,
    height: items.length * 50 + 50,
    state: {
        items,
    }
});
w.onClick(async event => {
    if (event.dataset.name) {
        const path: string = event.dataset.name;
        if (path === CLOSE_LABEL) {
            w.close();
            exit(0);  
        } else {
            
            await $`idea ${PROJECT_ROOT}/${path.slice(0, path.indexOf('/'))}`;
            
            await $`idea ${PROJECT_ROOT}/${path}`;
            
            
            await hide();
            await keyboard.pressKey(Key.LeftSuper, Key.F);
            await keyboard.releaseKey(Key.LeftSuper, Key.F);
            await keyboard.pressKey(Key.LeftSuper, Key.V);
            await keyboard.releaseKey(Key.LeftSuper, Key.V);
        }
    }
});```