"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _slate = require("slate");

var _debug = _interopRequireDefault(require("debug"));

var log = (0, _debug["default"])('@pie-lib:editable-html:image:insert-image-handler');
/**
 * Handles user selection, insertion (or cancellation) of an image into the editor.
 * @param {Block} placeholderBlock - a block that has been added to the editor as a place holder for the image
 * @param {Function} onFinish - a function to call if uploading fails or succeeds
 * @param {Function} getValue - a function to return the value of the editor
 * @param {Function} onChange - callback to notify changes applied by the handler
 * @param {Boolean} isPasted - a boolean that keeps track if the file is pasted
 */

var InsertImageHandler = /*#__PURE__*/function () {
  function InsertImageHandler(placeholderBlock, onFinish, getValue, onChange) {
    var isPasted = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
    (0, _classCallCheck2["default"])(this, InsertImageHandler);
    this.placeholderBlock = placeholderBlock;
    this.getValue = getValue;
    this.onFinish = onFinish;
    this.onChange = onChange;
    this.isPasted = isPasted;
    this.chosenFile = null;
  }

  (0, _createClass2["default"])(InsertImageHandler, [{
    key: "getPlaceholderInDocument",
    value: function getPlaceholderInDocument(value) {
      var document = value.document;
      var directChild = document.getChild(this.placeholderBlock.key);

      if (directChild) {
        return directChild;
      }

      var child = document.getDescendant(this.placeholderBlock.key);

      if (child) {
        return child;
      } else {
        //eslint-disable-next-line
        throw new Error("insert-image: Can't find placeholder!");
      }
    }
  }, {
    key: "cancel",
    value: function cancel() {
      log('insert cancelled');
      var c = this.getValue().change().removeNodeByKey(this.placeholderBlock.key);
      this.onChange(c);
      this.onFinish(false);
    }
  }, {
    key: "done",
    value: function done(err, src) {
      log('done: err:', err);

      if (err) {
        //eslint-disable-next-line
        console.log(err);
        this.onFinish(false);
      } else {
        var value = this.getValue();
        var child = this.getPlaceholderInDocument(value);
        var data = child.data.merge(_slate.Data.create({
          loaded: true,
          src: src,
          percent: 100
        }));
        var change = value.change().setNodeByKey(this.placeholderBlock.key, {
          data: data
        });
        this.onChange(change);
        this.onFinish(true);
      }
    }
    /**
     * Notify handler that the user chose a file - will create a change with a preview in the editor.
     *
     * @param {File} file - the file that the user chose using a file input.
     */

  }, {
    key: "fileChosen",
    value: function fileChosen(file) {
      var _this = this;

      if (!file) {
        return;
      } // Save the chosen file to this.chosenFile


      this.chosenFile = file;
      log('[fileChosen] file: ', file);
      var reader = new FileReader();

      reader.onload = function () {
        var value = _this.getValue();

        var dataURL = reader.result;

        var child = _this.getPlaceholderInDocument(value);

        var data = child.data.set('src', dataURL);
        var change = value.change().setNodeByKey(_this.placeholderBlock.key, {
          data: data
        });

        _this.onChange(change);
      };

      reader.readAsDataURL(file);
    }
  }, {
    key: "progress",
    value: function progress(percent, bytes, total) {
      log('progress: ', percent, bytes, total);
      var value = this.getValue();
      var child = this.getPlaceholderInDocument(value);
      var data = child.data.set('percent', percent);
      var change = value.change().setNodeByKey(this.placeholderBlock.key, {
        data: data
      });
      this.onChange(change);
    } // Add a getter method to retrieve the chosen file

  }, {
    key: "getChosenFile",
    value: function getChosenFile() {
      return this.chosenFile;
    }
  }]);
  return InsertImageHandler;
}();

var _default = InsertImageHandler;
exports["default"] = _default;
//# sourceMappingURL=insert-image-handler.js.map