"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular/core");
require("rxjs/add/operator/map");
const code_1 = require("../redux/code");
const project_1 = require("../redux/project");
const drag_1 = require("../util/drag");
const mouseButtons_1 = require("../util/mouseButtons");
/**
 * The code reizer component is a draggable bar that allows the user to
 * set the width of the code / display components.
 */
let CodeResizerComponent = CodeResizerComponent_1 = class CodeResizerComponent {
    constructor(el, project) {
        this.project = project;
        this.contentRoot = document.querySelector('editor-host > .content');
        this.el = el.nativeElement;
    }
    ngOnDestroy() {
        // noop
    }
    startDrag(startEv) {
        if (startEv instanceof MouseEvent && startEv.button !== mouseButtons_1.MouseButtons.Primary) {
            return;
        }
        this.el.classList.add('active');
        this.project.setFrameMasked(true);
        drag_1.captureDrag(startEv)
            .takeUntilDestroyed(this)
            .subscribe(ev => {
            this.updatePosition(ev);
        }, err => {
            throw err;
        }, () => {
            this.project.setFrameMasked(false);
            this.applyPosition();
        });
    }
    applyPosition() {
        if (this.wantsToHide) {
            this.project.setCodeState(code_1.CodeState.Closed);
            return;
        }
        this.project.setCodeWidth(this.nextWidth);
        this.el.style.transform = 'none';
        this.el.classList.remove('active', 'will-hide');
        this.wantsToHide = null;
        this.nextWidth = null;
    }
    updatePosition(ev) {
        const width = Math.min(this.getMaxWidth(), ev.pageX) - this.contentRoot.getBoundingClientRect().left;
        this.el.style.transform = `translateX(${width}px)`;
        this.nextWidth = width;
        const hide = this.shouldHide(width);
        if (hide !== this.wantsToHide) {
            this.wantsToHide = hide;
            this.el.classList.toggle('will-hide', hide);
        }
    }
    getMaxWidth() {
        return window.innerWidth * 0.66;
    }
    shouldHide(width) {
        return width < CodeResizerComponent_1.hideThreshold;
    }
};
/**
 * Width under which the code box will hide.
 */
CodeResizerComponent.hideThreshold = 300;
__decorate([
    core_1.HostListener('mousedown', ['$event']),
    core_1.HostListener('touchstart', ['$event']),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], CodeResizerComponent.prototype, "startDrag", null);
CodeResizerComponent = CodeResizerComponent_1 = __decorate([
    core_1.Component({
        selector: 'code-resizer',
        template: '',
        styleUrls: ['./code-resizer.component.scss'],
        changeDetection: core_1.ChangeDetectionStrategy.OnPush,
    }),
    __metadata("design:paramtypes", [core_1.ElementRef, project_1.ProjectService])
], CodeResizerComponent);
exports.CodeResizerComponent = CodeResizerComponent;
var CodeResizerComponent_1;
