import Ember from 'ember';
import layout from '../templates/components/ember-trix';
import _ from 'lodash/lodash';

const { Trix } = window;

const {
  computed,
  isNone,
  isPresent,
  set,
} = Ember;

const TRIX_EVENTS = [
  'trix-attachment-add',
  'trix-attachment-remove',
  'trix-blur',
  'trix-change',
  'trix-file-accept',
  'trix-focus',
  'trix-initialize',
  'trix-selection-change',
];

export default Ember.Component.extend({
  layout: layout,
  classNames: ['trix-editor-wrapper'],

  init() {
    this._super(...arguments);
    // merge config
    if (isPresent(this.attrs.config)) {
      Trix.config = _.merge(Trix.config, this.get('config'));
    }
  },

  $trix: computed('_$trix', {
    get() {
      let trix;
      if ( isNone(this.get('_$trix')) ) {
        trix = this.$('trix-editor');

        if ( isPresent(trix) ) {
          set(this, '_$trix', trix);
        }
      }
      return trix;
    }
  }),

  didInsertElement() {
    this._super(...arguments);
    TRIX_EVENTS.forEach(eventName => {
      let eventHandler = this.get(eventName);
      if (eventHandler) {
        this.get('$trix').on(eventName, event => {
          eventHandler(event.originalEvent);
        });
      }
    });
  },

  willDestroyElement() {
    this._super(...arguments);
    TRIX_EVENTS.forEach(eventName => {
      this.get('$trix').off(eventName);
    });
  },

});

@@ -1153,16 +1153,23 @@
         
+return 
 eventHan
@@ -1378,12 +1378,614 @@
 ;%0A  %7D,%0A%0A
+  drop(event) %7B%0A    let data = event.dataTransfer.getData('text/html');%0A    let editor = event.target.editor;%0A%0A    // look in parents for the trix editor%0A    if (!editor) %7B%0A      editor = $(event.target).parents('trix-editor')%5B0%5D%5B'editor'%5D;%0A    %7D%0A%0A    let src = '';%0A    const findImage = (event, el) =%3E %7B%0A      if ( $(el).is('img') ) %7B%0A        src = el.src;%0A      %7D%0A    %7D;%0A%0A    $(data).each(findImage);%0A%0A    if (src && editor) %7B%0A      let attributes = %7B url: src, contentType: 'image/png' %7D;%0A      let attachment = new Trix.Attachment(attributes);%0A      editor.insertAttachment(attachment);%0A    %7D%0A  %7D%0A%0A
 %7D);%0A

support dropping images