text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  var core_1 = require("@angular/core");
  var kendo_angular_common_1 = require("@progress/kendo-angular-common");
  var upload_service_1 = require("./upload.service");
  var util_1 = require("./util");
  var validation_util_1 = require("./validation-util");
  /**
   * @hidden
   */
  var DropZoneDirective = /** @class */ (function () {
      function DropZoneDirective(element, uploadService, _ngZone, renderer) {
          var _this = this;
          this.element = element;
          this.uploadService = uploadService;
          this._ngZone = _ngZone;
          this.renderer = renderer;
          this.hideIntervalDocument = null;
          this.hideIntervalElement = null;
          this.state = {
              active: "k-dropzone k-dropzone-active",
              hovered: "k-dropzone k-dropzone-active k-dropzone-hovered",
              inactive: "k-dropzone"
          };
          this.element = element;
          this.setClasses(this.state.inactive);
          if (!kendo_angular_common_1.isDocumentAvailable()) {
              return;
          }
          this._ngZone.runOutsideAngular(function () {
              _this.unsubscribeDocumentDragEnter = _this.renderer.listen('document', 'dragenter', function () { return _this.onDocumentDragEnter(); });
              _this.unsubscribeDocumentDragOver = _this.renderer.listen('document', 'dragover', function () { return _this.onDocumentDragOver(); });
          });
      }
      DropZoneDirective.prototype.ngOnDestroy = function () {
          var _this = this;
          this._ngZone.runOutsideAngular(function () {
              if (_this.unsubscribeDocumentDragEnter) {
                  _this.unsubscribeDocumentDragEnter();
              }
              if (_this.unsubscribeDocumentDragOver) {
                  _this.unsubscribeDocumentDragOver();
              }
          });
      };
      /**
       * @hidden
       */
      DropZoneDirective.prototype.onDocumentDragEnter = function () {
          var _this = this;
          if (this.lastDragElement) {
              if (this.calculateTimeDiff(this.lastDragElement) > 100) {
                  this.setClasses(this.state.active);
              }
          }
          else {
              this.setClasses(this.state.active);
          }
          this.lastDragDocument = new Date();
          if (!this.hideIntervalDocument) {
              this.hideIntervalDocument = setInterval(function () {
                  if (_this.calculateTimeDiff(_this.lastDragDocument) < 100) {
                      return;
                  }
                  _this.setClasses(_this.state.inactive);
                  clearInterval(_this.hideIntervalDocument);
                  _this.hideIntervalDocument = null;
              }, 100);
          }
          return false;
      };
      /**
       * @hidden
       */
      DropZoneDirective.prototype.onDocumentDragOver = function () {
          this.lastDragDocument = new Date();
          return false;
      };
      /**
       * @hidden
       */
      DropZoneDirective.prototype.onElementDragEnterListener = function () {
          var _this = this;
          this.setClasses(this.state.hovered);
          this.lastDragElement = new Date();
          if (!this.hideIntervalElement) {
              this.hideIntervalElement = setInterval(function () {
                  if (_this.calculateTimeDiff(_this.lastDragElement) < 100) {
                      return;
                  }
                  if (_this.lastDrop) {
                      var diff = _this.calculateTimeDiff(_this.lastDrop);
                      _this.setClasses(diff > 200 ? _this.state.active : _this.state.inactive);
                  }
                  else {
                      _this.setClasses(_this.state.active);
                  }
                  clearInterval(_this.hideIntervalElement);
                  _this.hideIntervalElement = null;
              }, 100);
          }
          return false;
      };
      /**
       * @hidden
       */
      DropZoneDirective.prototype.onElementDragOverListener = function () {
          this.lastDragElement = new Date();
          return false;
      };
      /**
       * @hidden
       */
      DropZoneDirective.prototype.onDropListener = function (event) {
          this.lastDrop = new Date();
          var droppedFiles = event.dataTransfer.files;
          if (droppedFiles.length > 0 && !this.disabled) {
              var files = util_1.getAllFileInfo(droppedFiles);
              files = util_1.assignGuidToFiles(files, !this.async.batch);
              if (!this.multiple) {
                  files.splice(1, files.length - 1);
                  this.uploadService.clearFiles();
              }
              validation_util_1.default.validateFiles(files, this.restrictions);
              this.uploadService.addFiles(files, this.async);
          }
          return false;
      };
      DropZoneDirective.prototype.calculateTimeDiff = function (prevEvent) {
          return new Date().getTime() - prevEvent.getTime();
      };
      DropZoneDirective.prototype.setClasses = function (classes) {
          if (!this.disabled) {
              this.element.nativeElement.className = classes;
          }
      };
      Object.defineProperty(DropZoneDirective.prototype, "initialClassName", {
          /**
           * @hidden
           */
          get: function () {
              return true;
          },
          enumerable: true,
          configurable: true
      });
      DropZoneDirective.decorators = [
          { type: core_1.Directive, args: [{
                      selector: '[kendoUploadDropzone]'
                  },] },
      ];
      /** @nocollapse */
      DropZoneDirective.ctorParameters = function () { return [
          { type: core_1.ElementRef },
          { type: upload_service_1.UploadService },
          { type: core_1.NgZone },
          { type: core_1.Renderer2 }
      ]; };
      DropZoneDirective.propDecorators = {
          async: [{ type: core_1.Input }],
          disabled: [{ type: core_1.Input }],
          multiple: [{ type: core_1.Input }],
          restrictions: [{ type: core_1.Input }],
          onElementDragEnterListener: [{ type: core_1.HostListener, args: ['dragenter',] }],
          onElementDragOverListener: [{ type: core_1.HostListener, args: ['dragover',] }],
          onDropListener: [{ type: core_1.HostListener, args: ['drop', ['$event'],] }],
          initialClassName: [{ type: core_1.HostBinding, args: ['class.k-dropzone',] }]
      };
      return DropZoneDirective;
  }());
  exports.DropZoneDirective = DropZoneDirective;
  