'use strict';

var Component = require('../../ui/Component');
var clone = require('lodash/clone');
var Prompt = require('../../ui/Prompt');

/**
  Component to edit an existing link.

  Designed so that it can be used either in a toolbar, or within
  an overlay on the Surface.
*/
function EditLinkTool() {
  EditLinkTool.super.apply(this, arguments);
}

EditLinkTool.Prototype = function() {

  this.render = function($$) {
    // var node = this.props.node;
    var el = $$('div').addClass('sc-edit-link-tool');

    el.append(
      $$(Prompt).append(
        // $$(Prompt.Input, {path: [node.id, 'url']}),
        $$(Prompt.Label, {label: 'TODO: provide link editor'}),
        $$(Prompt.Separator),
        $$(Prompt.Action, {name: 'open-link', title: this.i18n.t('open-link')}),
        $$(Prompt.Action, {name: 'delete', title: this.i18n.t('delete')})
          .on('click', this.onDelete)
      )
    );
    return el;
  };

  this.onSave = function(e) {
    e.preventDefault();

    var node = this.props.node;
    var documentSession = this.context.documentSession;
    var url = this.refs.url.val();

    documentSession.transaction(function(tx) {
      tx.set([node.id, "url"], url);
    });
  };

  this.onDelete = function(e) {
    e.preventDefault();
    var node = this.props.node;
    var documentSession = this.context.documentSession;
    documentSession.transaction(function(tx) {
      tx.delete(node.id);
    });
  };
};

Component.extend(EditLinkTool);

EditLinkTool.static.getProps = function(commandStates) {
  if (commandStates.link.mode === 'edit') {
    return clone(commandStates.link);
  } else {
    return undefined;
  }
};

EditLinkTool.static.name = 'edit-link';

module.exports = EditLinkTool;
@@ -421,19 +421,16 @@
 $) %7B%0A   
- //
  var nod
@@ -556,11 +556,8 @@
     
- //
  $$(
@@ -571,16 +571,50 @@
 Input, %7B
+%0A          type: 'url',%0A          
 path: %5Bn
@@ -627,18 +627,16 @@
 , 'url'%5D
-%7D)
 ,%0A      
@@ -641,139 +641,182 @@
     
-$$(Prompt.Label, %7Blabel: 'TODO: provide link editor'%7D),%0A        $$(Prompt.Separator),%0A        $$(Prompt.Action, %7Bname: 'open-link',
+  placeholder: 'Paste or type a link url'%0A        %7D),%0A        $$(Prompt.Separator),%0A        $$(Prompt.Link, %7B%0A          name: 'open-link',%0A          href: node.url,%0A         
  tit
@@ -843,16 +843,25 @@
 n-link')
+%0A        
 %7D),%0A    

Bring back link editing.