const version = "v11"
//console.info("WINDOW TAGGER STARTED - " + version)

const windows = Array.from({length: 9}, () => null)

for (let i = 0; i < 9; i++) {
  registerShortcut(
    `TagWindow${i+1}`,
    `Tag current window to tag ${i+1}`,
    `Meta+Shift+${i+1}`,
    function() {
      try {
        //console.info(`Trying to tag at ${i+1}`)
        if (!workspace.activeWindow) {
          //console.info("No active window to tag")
          return
        }
        
        //console.info(`Tag ${i+1}: ${workspace.activeWindow.caption}`)
        for (let j = 0; j < 9; j++) {
          if (windows[j] === workspace.activeWindow) {
            windows[j] = null;
          }
        }
        windows[i] = workspace.activeWindow
      } catch (e) {
        console.info(e)
      }
    }
  );

  registerShortcut(
    `FocusWindow${i}`,
    `Focus Window at tag ${i+1}`,
    `Meta+${i+1}`,
    function() {
      try {
        //console.info(`Total: ${windows.filter(w => w !== null).length}`)
        windows.forEach(w => {
          if (w) {
            //console.info(`- ${w.caption}`)
          }
        })
        if (!windows[i]) {
          //console.info("Tag is empty")
          return
        }

        if (windows[i] === workspace.activeWindow) {
          windows[i].minimized = true
          //console.info("Focusing already focused window")
          return
        }

        try {
          workspace.activeWindow = windows[i]
        } catch (error ) {
          // console.info(windows[i].valid, windows[i].deleted)
          // console.info("Error: ", error)
          windows[i] = null
        }
      } catch (e) {
        // console.info(e)
      }
    }
  );
}

