/**
 * An element which display an inline editor for writing and previewing markdown text.
 */
angular.module('quay').directive('markdownEditor', function () {
  var counter = 0;

  var directiveDefinitionObject = {
    priority: 0,
    templateUrl: '/static/directives/markdown-editor.html',
    replace: false,
    transclude: false,
    restrict: 'C',
    scope: {
      'content': '=content',
    },
    controller: function($scope, $element, $timeout) {
      $scope.id = (counter++);
      $scope.previewing = false;

      $timeout(function() {
        var converter = Markdown.getSanitizingConverter();
        var editor = new Markdown.Editor(converter, '-' + $scope.id);
        editor.run();
      });

      $scope.togglePreview = function() {
        $scope.previewing = !$scope.previewing;
      };
    }
  };
  return directiveDefinitionObject;
});