<commit_before>$ = require("jquery")
Bacon = require("baconjs")
_ = require("lodash")
examples = require("./examples.coffee")
assetUploader = require("./asset-uploader.coffee")
AssetListView = require("./asset-list-view.coffee")
Editor = require("./editor.coffee")
ErrorDisplay = require("./error-display.coffee")
runCode = require("./code-runner.coffee")
menubar = require("./menu.coffee")
storage = require("./storage.coffee")
Author = require("./author.coffee")
FileDialog = require("./file-dialog.coffee")
showStatusMessage = require("./status-message.coffee")

module.exports = (initialApplication) ->
  fileLoadedBus = new Bacon.Bus()
  assetsP = Bacon.update initialApplication.assets,
    assetUploader.newAssetE, (assets, newFile) ->
      assets = _.clone(assets)
      assets[newFile.name] = newFile
      assets
    assetUploader.removeAssetE, (assets, asset) ->
      assets = _.clone(assets)
      delete assets[asset.name]
      assets
    fileLoadedBus.map(".assets"), (_, loadedAssets) ->
      loadedAssets

  editor = Editor(initialApplication.code, fileLoadedBus.map(".code"))

  logoutE = menubar.itemClickE("file-logout")
  author = Author(initialApplication.author, menubar.itemClickE("file-login"), logoutE)

  logoutE.map("Logged out").onValue(showStatusMessage)
  author.authorP.changes().filter(author.loggedInP).map((author) -> "Logged in as \"" + author + "\"")
    .onValue(showStatusMessage)

  fileDialog = FileDialog(author.authorP, menubar.itemClickE("file-open"))
  fileDialog.fileLoadedE.map((app) -> "Loaded \"" + app.name + "\"").onValue(showStatusMessage)
  fileLoadedBus.plug(fileDialog.fileLoadedE)
  fileLoadedBus.plug(menubar.itemClickE("file-new").map(examples.empty))


  nameModE = fileLoadedBus.map((app) -> (-> {name: app.name}))
    .merge(menubar.itemClickE("file-rename").map(-> ({name}, newName) ->
       promptNewName(name).map((newName) -> {name: newName, rename: true, oldName: name})
     ))

  nameChangesP = nameModE.flatScan(initialApplication, (oldName, f) -> f(oldName))
  nameP = nameChangesP.map(".name")

  renameE = nameChangesP.changes().filter(".rename").map(({name, oldName}) -> {newName: name, oldName})

  nameP.onValue (name) ->
    $("#menu-file > .title").text('Project "' + name + '"')

  applicationP = Bacon.combineTemplate
    author: author.authorP
    name: nameP
    code: editor.codeP
    assets: assetsP
<commit_msg>don't derive author from initially loaded app<commit_after>$ = require("jquery")
Bacon = require("baconjs")
_ = require("lodash")
examples = require("./examples.coffee")
assetUploader = require("./asset-uploader.coffee")
AssetListView = require("./asset-list-view.coffee")
Editor = require("./editor.coffee")
ErrorDisplay = require("./error-display.coffee")
runCode = require("./code-runner.coffee")
menubar = require("./menu.coffee")
storage = require("./storage.coffee")
Author = require("./author.coffee")
FileDialog = require("./file-dialog.coffee")
showStatusMessage = require("./status-message.coffee")

module.exports = (initialApplication) ->
  fileLoadedBus = new Bacon.Bus()
  assetsP = Bacon.update initialApplication.assets,
    assetUploader.newAssetE, (assets, newFile) ->
      assets = _.clone(assets)
      assets[newFile.name] = newFile
      assets
    assetUploader.removeAssetE, (assets, asset) ->
      assets = _.clone(assets)
      delete assets[asset.name]
      assets
    fileLoadedBus.map(".assets"), (_, loadedAssets) ->
      loadedAssets

  editor = Editor(initialApplication.code, fileLoadedBus.map(".code"))

  logoutE = menubar.itemClickE("file-logout")
  author = Author(null, menubar.itemClickE("file-login"), logoutE)

  logoutE.map("Logged out").onValue(showStatusMessage)
  author.authorP.changes().filter(author.loggedInP).map((author) -> "Logged in as \"" + author + "\"")
    .onValue(showStatusMessage)

  fileDialog = FileDialog(author.authorP, menubar.itemClickE("file-open"))
  fileDialog.fileLoadedE.map((app) -> "Loaded \"" + app.name + "\"").onValue(showStatusMessage)
  fileLoadedBus.plug(fileDialog.fileLoadedE)
  fileLoadedBus.plug(menubar.itemClickE("file-new").map(examples.empty))


  nameModE = fileLoadedBus.map((app) -> (-> {name: app.name}))
    .merge(menubar.itemClickE("file-rename").map(-> ({name}, newName) ->
       promptNewName(name).map((newName) -> {name: newName, rename: true, oldName: name})
     ))

  nameChangesP = nameModE.flatScan(initialApplication, (oldName, f) -> f(oldName))
  nameP = nameChangesP.map(".name")

  renameE = nameChangesP.changes().filter(".rename").map(({name, oldName}) -> {newName: name, oldName})

  nameP.onValue (name) ->
    $("#menu-file > .title").text('Project "' + name + '"')

  applicationP = Bacon.combineTemplate
    author: author.authorP
    name: nameP
    code: editor.codeP
    assets: assetsP

{'fraction_non_alphanumeric': 0.17217716781035558, 'fraction_numerical': 0.0, 'mean_word_length': 4.4411764705882355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '4700011', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1546, 'n_words': 250}