/*
 * Copyright (C) 2016-2020 Kano Computing Ltd.
 * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 */
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;
};
import { LitElement, customElement, html, property, css, query } from 'lit-element/lit-element.js';
import { subscribeDOM } from '@kano/common/index.js';
let KcEditableNumber = class KcEditableNumber extends LitElement {
    constructor() {
        super(...arguments);
        this.value = 0;
        this.editing = false;
    }
    static get styles() {
        return [css `
            label {
                cursor: col-resize;
                user-select: none;
                color: #1565c0;
                text-decoration: underline;
            }
            input {
                text-align: right;
                font-family: inherit;
                font-size: inherit;
                background: transparent;
                color: white;
                border: 1px solid grey;
                outline: none;
                padding: 2px;
                margin: -3px;
            }
            input[type="number"]::-webkit-outer-spin-button, input[type="number"]::-webkit-inner-spin-button {
                -webkit-appearance: none;
                margin: 0;
            }
            input[type="number"] {
                -moz-appearance: textfield;
            }
        `];
    }
    render() {
        return html `
            ${this.editing ? this.renderInput() : this.renderLabel()}
        `;
    }
    renderLabel() {
        return html `<label @mousedown=${this.onMouseDown}>${this.value.toFixed(2)}</label>`;
    }
    renderInput() {
        return html `<input type="number" .value=${this.value.toFixed(2)} @keydown=${this._onInputKeyDown} @blur=${this._onInputBlur} />`;
    }
    onMouseDown(e) {
        const start = e.x;
        let lastValue = start;
        const moveSub = subscribeDOM(document, 'mousemove', (e) => {
            const delta = e.x - lastValue;
            let step = 1;
            if (e.shiftKey) {
                step = 5;
            }
            else if (e.ctrlKey) {
                step = 0.1;
            }
            this.value += delta * step;
            lastValue = e.x;
            this.dispatchEvent(new CustomEvent('input', { detail: this.value, bubbles: true, composed: true }));
        });
        const upSub = subscribeDOM(document, 'mouseup', () => {
            moveSub.dispose();
            upSub.dispose();
            if (lastValue === start) {
                this._onClick();
            }
            else {
                this.dispatchEvent(new CustomEvent('change', { detail: this.value, bubbles: true, composed: true }));
            }
        });
    }
    _onClick() {
        this.editing = true;
        this.updateComplete.then(() => {
            const input = this.inputEl;
            input.select();
        });
    }
    _onInputKeyDown(e) {
        if (e.keyCode === 27) {
            this.cancel();
        }
        if (e.keyCode === 13) {
            this.apply();
        }
    }
    _onInputBlur() {
        this.apply();
    }
    cancel() {
        this.editing = false;
    }
    apply() {
        const value = parseFloat(this.inputEl.value);
        this.editing = false;
        if (value !== this.value) {
            this.value = value;
            this.dispatchEvent(new CustomEvent('change', { detail: this.value, bubbles: true, composed: true }));
        }
    }
};
__decorate([
    property({ type: Number })
], KcEditableNumber.prototype, "value", void 0);
__decorate([
    property({ type: Boolean })
], KcEditableNumber.prototype, "editing", void 0);
__decorate([
    query('input')
], KcEditableNumber.prototype, "inputEl", void 0);
__decorate([
    query('label')
], KcEditableNumber.prototype, "labelEl", void 0);
KcEditableNumber = __decorate([
    customElement('kc-editable-number')
], KcEditableNumber);
export { KcEditableNumber };
//# sourceMappingURL=kc-editable-number.js.map