<commit_before>kd = require 'kd'
nick = require 'app/util/nick'
whoami = require 'app/util/whoami'
registerRoutes = require 'app/util/registerRoutes'
globals = require 'globals'
remote = require('app/remote').getInstance()
Machine = require 'app/providers/machine'
lazyrouter = require 'app/lazyrouter'
dataProvider = require 'app/userenvironmentdataprovider'


selectWorkspaceOnSidebar = (data) ->

  { machine, workspace } = data

  return  unless machine or workspace

  kd.getSingleton('mainView').activitySidebar.selectWorkspace data
  storage = kd.singletons.localStorageController.storage 'IDE'

  workspaceData    =
    machineLabel   : machine.slug or machine.label
    workspaceSlug  : workspace.slug
    channelId      : workspace.channelId

  storage.setValue 'LatestWorkspace', workspaceData
  storage.setValue "LatestWorkspace_#{machine.uid}", workspaceData


getLatestWorkspace = (machine) ->

  storage   = kd.getSingleton('localStorageController').storage 'IDE'
  if machine
    workspace = storage.getValue "LatestWorkspace_#{machine.uid}"

  unless machine and workspace
    workspace = storage.getValue 'LatestWorkspace'

  return  unless workspace

  { machineLabel, workspaceSlug, channelId } = workspace

  if dataProvider.findWorkspace machineLabel, workspaceSlug, channelId
    return workspace


loadIDENotFound = ->

  {appManager} = kd.singletons
  appManager.open 'IDE', { forceNew: yes }, (app) ->
    app.amIHost = yes
    appManager.tell 'IDE', 'createMachineStateModal', state: 'NotFound'


loadIDE = (data) ->

  { machine, workspace, username, channelId } = data
  selectWorkspaceOnSidebar data

  appManager = kd.getSingleton 'appManager'
  ideApps    = appManager.appControllers.IDE
  machineUId = machine.uid
  callback   = ->
    appManager.open 'IDE', { forceNew: yes }, (app) ->
      app.mountedMachineUId   = machineUId
      app.workspaceData       = workspace

      if username and username isnt nick()
        app.isInSession       = yes
        app.amIHost           = no
        app.collaborationHost = username
        app.channelId         = channelId
      else
        app.amIHost           = yes

      appManager.tell 'IDE', 'mountMachineByMachineUId', machineUId

  return callback()  unless ideApps?.instances

  for instance in ideApps.instances
    isSameMachine   = instance.mountedMachineUId is machineUId
    isSameWorkspace = instance.workspaceData?.getId() is workspace.getId()

    if isSameMachine
      if isSameWorkspace then ideInstance = instance
      # should not be the case anymore since 'my-workspace' deprecated.
      else if workspace.slug is 'my-workspace'
        if instance.workspaceData?.isDefault
          ideInstance = instance

  if ideInstance
    appManager.showInstance ideInstance
    selectWorkspaceOnSidebar data # should not be required
  else
    callback()


routeToFallback = ->

  machines = dataProvider.getMyMachines()
  router   = kd.getSingleton 'router'
  [ obj ]  = machines

  if obj?.machine # `?` intentionally. there might be no machine.
    routeToMachineWorkspace obj.machine
  else
    loadIDENotFound()


routeToMachineWorkspace = (machine) ->

  latestWorkspace = getLatestWorkspace machine
  workspaceSlug   = 'my-workspace'

  if latestWorkspace
    { machineLabel } = latestWorkspace
    if machineLabel is machine.label
      workspaceSlug = latestWorkspace.workspaceSlug

  kd.getSingleton('router').handleRoute "/IDE/#{machine.slug}/#{workspaceSlug}"


routeToLatestWorkspace = ->

  router          = kd.getSingleton 'router'
  latestWorkspace = getLatestWorkspace()

  return routeToFallback()  unless latestWorkspace

  { machineLabel, workspaceSlug, channelId } = latestWorkspace

  if channelId
    dataProvider.fetchMachineAndWorkspaceByChannelId channelId, (machine, ws) =>
      if machine and ws then router.handleRoute "/IDE/#{channelId}"
      else routeToFallback()

  else if machineLabel and workspaceSlug
    dataProvider.fetchMachineByLabel machineLabel, (machine, workspace) =>
      if machine and workspace
        router.handleRoute "/IDE/#{machineLabel}/#{workspaceSlug}"
      else if machine
        routeToMachineWorkspace machine
      else
        routeToFallback()


loadCollaborativeIDE = (id) ->
<commit_msg>Routes: Change machine route for permanent shared vms.<commit_after>kd = require 'kd'
nick = require 'app/util/nick'
whoami = require 'app/util/whoami'
registerRoutes = require 'app/util/registerRoutes'
globals = require 'globals'
remote = require('app/remote').getInstance()
Machine = require 'app/providers/machine'
lazyrouter = require 'app/lazyrouter'
dataProvider = require 'app/userenvironmentdataprovider'


selectWorkspaceOnSidebar = (data) ->

  { machine, workspace } = data

  return  unless machine or workspace

  kd.getSingleton('mainView').activitySidebar.selectWorkspace data
  storage = kd.singletons.localStorageController.storage 'IDE'

  workspaceData    =
    machineLabel   : machine.slug or machine.label
    workspaceSlug  : workspace.slug
    channelId      : workspace.channelId

  storage.setValue 'LatestWorkspace', workspaceData
  storage.setValue "LatestWorkspace_#{machine.uid}", workspaceData


getLatestWorkspace = (machine) ->

  storage   = kd.getSingleton('localStorageController').storage 'IDE'
  if machine
    workspace = storage.getValue "LatestWorkspace_#{machine.uid}"

  unless machine and workspace
    workspace = storage.getValue 'LatestWorkspace'

  return  unless workspace

  { machineLabel, workspaceSlug, channelId } = workspace

  if dataProvider.findWorkspace machineLabel, workspaceSlug, channelId
    return workspace


loadIDENotFound = ->

  {appManager} = kd.singletons
  appManager.open 'IDE', { forceNew: yes }, (app) ->
    app.amIHost = yes
    appManager.tell 'IDE', 'createMachineStateModal', state: 'NotFound'


loadIDE = (data) ->

  { machine, workspace, username, channelId } = data
  selectWorkspaceOnSidebar data

  appManager = kd.getSingleton 'appManager'
  ideApps    = appManager.appControllers.IDE
  machineUId = machine.uid
  callback   = ->
    appManager.open 'IDE', { forceNew: yes }, (app) ->
      app.mountedMachineUId   = machineUId
      app.workspaceData       = workspace

      if username and username isnt nick()
        app.isInSession       = yes
        app.amIHost           = no
        app.collaborationHost = username
        app.channelId         = channelId
      else
        app.amIHost           = yes

      appManager.tell 'IDE', 'mountMachineByMachineUId', machineUId

  return callback()  unless ideApps?.instances

  for instance in ideApps.instances
    isSameMachine   = instance.mountedMachineUId is machineUId
    isSameWorkspace = instance.workspaceData?.getId() is workspace.getId()

    if isSameMachine
      if isSameWorkspace then ideInstance = instance
      # should not be the case anymore since 'my-workspace' deprecated.
      else if workspace.slug is 'my-workspace'
        if instance.workspaceData?.isDefault
          ideInstance = instance

  if ideInstance
    appManager.showInstance ideInstance
    selectWorkspaceOnSidebar data # should not be required
  else
    callback()


routeToFallback = ->

  machines = dataProvider.getMyMachines()
  router   = kd.getSingleton 'router'
  [ obj ]  = machines

  if obj?.machine # `?` intentionally. there might be no machine.
    routeToMachineWorkspace obj.machine
  else
    loadIDENotFound()


routeToMachineWorkspace = (machine) ->

  latestWorkspace = getLatestWorkspace machine
  workspaceSlug   = 'my-workspace'

  if latestWorkspace
    { machineLabel } = latestWorkspace
    if machineLabel is machine.label
      workspaceSlug = latestWorkspace.workspaceSlug

  identifier = if machine.isPermanent() then machine.uid else machine.slug
  kd.getSingleton('router').handleRoute "/IDE/#{identifier}/#{workspaceSlug}"


routeToLatestWorkspace = ->

  router          = kd.getSingleton 'router'
  latestWorkspace = getLatestWorkspace()

  return routeToFallback()  unless latestWorkspace

  { machineLabel, workspaceSlug, channelId } = latestWorkspace

  if channelId
    dataProvider.fetchMachineAndWorkspaceByChannelId channelId, (machine, ws) =>
      if machine and ws then router.handleRoute "/IDE/#{channelId}"
      else routeToFallback()

  else if machineLabel and workspaceSlug
    dataProvider.fetchMachineByLabel machineLabel, (machine, workspace) =>
      if machine and workspace
        router.handleRoute "/IDE/#{machineLabel}/#{workspaceSlug}"
      else if machine
        routeToMachineWorkspace machine
      else
        routeToFallback()


loadCollaborativeIDE = (id) ->

{'fraction_non_alphanumeric': 0.08520751217250173, 'fraction_numerical': 0.0, 'mean_word_length': 3.566966649020646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3801320', 'n_tokens_mistral': 2463, 'n_tokens_neox': 2463, 'n_words': 602}