<commit_before># = require ../files
# = require ./dragdrop
# = require ./template
# = require ./dialog

{
  namespace,
  utils,
  uploads,
  jQuery: $
} = uploadcare

{t} = uploadcare.locale

namespace 'uploadcare.widget', (ns) ->
  class ns.Widget

    constructor: (element) ->
      @element = $(element)
      @settings = utils.buildSettings @element.data()
      @__onChange = $.Callbacks()
      @onChange = utils.publicCallbacks @__onChange

      # Shortcut for:
      #   widget.onChange (file) ->
      #     file.done (info) -> ...
      __onUploadComplete = $.Callbacks()
      @onUploadComplete = utils.publicCallbacks __onUploadComplete
      @__onChange.add (file) =>
        file?.done (info) =>
          __onUploadComplete.fire info

      @__setupWidget()
      @currentFile = null
      @template.reset()

      @element.on 'change.uploadcare', => @reloadInfo()
      @reloadInfo()

    __reset: (keepValue=false) =>
      @currentFile?.cancel()
      @currentFile = null
      @template.reset()
      unless keepValue
        @__setValue ''

    __setFile: (newFile, keepValue=false) =>
      if newFile == @currentFile
        if newFile
          @__updateValue() unless keepValue
        return
      @__reset(keepValue)
      if newFile
        @currentFile = newFile
        @template.listen @currentFile
        @currentFile
          .fail (error) =>
            if newFile == @currentFile
              @__fail error
          .done (info) =>
            if newFile == @currentFile
              @template.setFileInfo(info)
              @template.loaded()
        @__updateValue() unless keepValue

    __updateValue: ->
      file = @currentFile
      @currentFile.done (info) =>
        if file == @currentFile
          @__setValue if info.cdnUrlModifiers || @settings.pathValue
            info.cdnUrl
          else
            info.uuid

    __setValue: (value) ->
      if @element.val() != value
        @element.val(value)
        @__onChange.fire @currentFile

    value: (value) ->
      if value?
        if @element.val() != value
          @__setFile(
            if value.done && value.fail
              value
            else
              uploadcare.fileFrom('url', value, @settings)
          )
        this
      else
        @currentFile

    reloadInfo: =>
      if @element.val()
        file = uploadcare.fileFrom('url', @element.val(), @settings)
        @__setFile file, true
      else
        @__reset()
      this

    __fail: (error) =>
      @__reset()
      @template.error error

    __setupWidget: ->
      @template = new ns.Template(@settings, @element)

      @template.addButton('cancel', t('buttons.cancel')).on('click', => @__reset())
      @template.addButton('remove', t('buttons.remove')).on('click', => @__reset())

      # Create the dialog and widget buttons
      if @settings.tabs.length > 0
        if 'file' in @settings.tabs
          fileButton = @template.addButton('file')
          fileButton.on 'click', =>
            @openDialog('file')

        dialogButton = @template.addButton('dialog')
        dialogButton.on 'click', => @openDialog()


      # Enable drag and drop
      ns.dragdrop.receiveDrop(@__openDialogWithFile, @template.dropArea)
      @template.dropArea.on 'dragstatechange.uploadcare', (e, active) =>
        unless active && uploadcare.isDialogOpened()
          @template.dropArea.toggleClass('uploadcare-dragging', active)

      @template.content.on 'click', '@uploadcare-widget-file-name', =>
        @openDialog()

    __openDialogWithFile: (type, data) =>
      file = uploadcare.fileFrom(type, data, @settings)
      uploadcare.openDialog(file, @settings).done(@__setFile)

    openDialog: (tab) ->
      uploadcare.openDialog(@currentFile, tab, @settings)
        .done(@__setFile)
        .fail (file) =>
          unless file == @currentFile
            @__setFile null

    api: ->
      unless @__api
        @__api = utils.bindAll this, [
          'openDialog'
          'reloadInfo'
          'value'
        ]
        @__api.onChange = @onChange
        @__api.onUploadComplete = @onUploadComplete
      @__api<commit_msg>do not show dialog after direct file selection if `preview-step` turned off<commit_after># = require ../files
# = require ./dragdrop
# = require ./template
# = require ./dialog

{
  namespace,
  utils,
  uploads,
  jQuery: $
} = uploadcare

{t} = uploadcare.locale

namespace 'uploadcare.widget', (ns) ->
  class ns.Widget

    constructor: (element) ->
      @element = $(element)
      @settings = utils.buildSettings @element.data()
      @__onChange = $.Callbacks()
      @onChange = utils.publicCallbacks @__onChange

      # Shortcut for:
      #   widget.onChange (file) ->
      #     file.done (info) -> ...
      __onUploadComplete = $.Callbacks()
      @onUploadComplete = utils.publicCallbacks __onUploadComplete
      @__onChange.add (file) =>
        file?.done (info) =>
          __onUploadComplete.fire info

      @__setupWidget()
      @currentFile = null
      @template.reset()

      @element.on 'change.uploadcare', => @reloadInfo()
      @reloadInfo()

    __reset: (keepValue=false) =>
      @currentFile?.cancel()
      @currentFile = null
      @template.reset()
      unless keepValue
        @__setValue ''

    __setFile: (newFile, keepValue=false) =>
      if newFile == @currentFile
        if newFile
          @__updateValue() unless keepValue
        return
      @__reset(keepValue)
      if newFile
        @currentFile = newFile
        @template.listen @currentFile
        @currentFile
          .fail (error) =>
            if newFile == @currentFile
              @__fail error
          .done (info) =>
            if newFile == @currentFile
              @template.setFileInfo(info)
              @template.loaded()
        @__updateValue() unless keepValue

    __updateValue: ->
      file = @currentFile
      @currentFile.done (info) =>
        if file == @currentFile
          @__setValue if info.cdnUrlModifiers || @settings.pathValue
            info.cdnUrl
          else
            info.uuid

    __setValue: (value) ->
      if @element.val() != value
        @element.val(value)
        @__onChange.fire @currentFile

    value: (value) ->
      if value?
        if @element.val() != value
          @__setFile(
            if value.done && value.fail
              value
            else
              uploadcare.fileFrom('url', value, @settings)
          )
        this
      else
        @currentFile

    reloadInfo: =>
      if @element.val()
        file = uploadcare.fileFrom('url', @element.val(), @settings)
        @__setFile file, true
      else
        @__reset()
      this

    __fail: (error) =>
      @__reset()
      @template.error error

    __setupWidget: ->
      @template = new ns.Template(@settings, @element)

      @template.addButton('cancel', t('buttons.cancel')).on('click', => @__reset())
      @template.addButton('remove', t('buttons.remove')).on('click', => @__reset())

      # Create the dialog and widget buttons
      if @settings.tabs.length > 0
        if 'file' in @settings.tabs
          fileButton = @template.addButton('file')
          fileButton.on 'click', =>
            @openDialog('file')

        dialogButton = @template.addButton('dialog')
        dialogButton.on 'click', => @openDialog()


      # Enable drag and drop
      ns.dragdrop.receiveDrop(@__openDialogWithFile, @template.dropArea)
      @template.dropArea.on 'dragstatechange.uploadcare', (e, active) =>
        unless active && uploadcare.isDialogOpened()
          @template.dropArea.toggleClass('uploadcare-dragging', active)

      @template.content.on 'click', '@uploadcare-widget-file-name', =>
        @openDialog()

    __openDialogWithFile: (type, data) =>
      file = uploadcare.fileFrom(type, data, @settings)
      if @settings.previewStep
        uploadcare.openDialog(file, @settings).done(@__setFile)
      else
        @__setFile file

    openDialog: (tab) ->
      uploadcare.openDialog(@currentFile, tab, @settings)
        .done(@__setFile)
        .fail (file) =>
          unless file == @currentFile
            @__setFile null

    api: ->
      unless @__api
        @__api = utils.bindAll this, [
          'openDialog'
          'reloadInfo'
          'value'
        ]
        @__api.onChange = @onChange
        @__api.onUploadComplete = @onUploadComplete
      @__api
{'fraction_non_alphanumeric': 0.12592064623425991, 'fraction_numerical': 0.00023758612497030174, 'mean_word_length': 2.105496126890446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 61, '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': '4936844', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2405, 'n_words': 521}