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);
};
import { Component, forwardRef, Input } from '@angular/core';
import { FormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms';
var MoneyGbpInputComponent = /** @class */ (function () {
    function MoneyGbpInputComponent() {
        this.displayValue = null;
        this.propagateChange = function (_) { };
    }
    MoneyGbpInputComponent_1 = MoneyGbpInputComponent;
    // change events from the textarea
    MoneyGbpInputComponent.prototype.onChange = function (event) {
        // get value from input
        var newValue = event.target.value;
        if (newValue && MoneyGbpInputComponent_1.PATTERN_REGEXP.test(newValue)) {
            var parts = newValue.split('.');
            if (!parts[1]) {
                parts[1] = '00';
            }
            else {
                while (2 > parts[1].length) {
                    parts[1] += '0';
                }
            }
            this.rawValue = parts.join('');
        }
        else {
            // When pattern not matched, value is passed as is so that it fails validation.
            this.rawValue = newValue;
        }
        // update the form
        this.propagateChange(this.rawValue);
    };
    MoneyGbpInputComponent.prototype.writeValue = function (obj) {
        if (obj) {
            this.rawValue = obj;
            var integerPart = obj.slice(0, -2) || '0';
            var decimalPart = obj.slice(-2);
            while (2 > decimalPart.length) {
                decimalPart += '0';
            }
            this.displayValue = [integerPart, decimalPart].join('.');
        }
    };
    MoneyGbpInputComponent.prototype.registerOnChange = function (fn) {
        this.propagateChange = fn;
    };
    MoneyGbpInputComponent.prototype.registerOnTouched = function (_) {
        // Not used.
    };
    MoneyGbpInputComponent.prototype.setDisabledState = function (isDisabled) {
        this.disabled = isDisabled;
    };
    MoneyGbpInputComponent.prototype.onBlur = function () {
        this.formControl.markAsTouched();
        this.propagateChange(this.rawValue);
    };
    MoneyGbpInputComponent.prototype.validate = function (control) {
        if (this.mandatory && !control.value) {
            return {
                pattern: 'This field is required'
            };
        }
        if (control.value && !MoneyGbpInputComponent_1.PATTERN_REGEXP.test(control.value)) {
            return {
                pattern: 'Should only contain numbers with up to 2 decimal places'
            };
        }
        return undefined;
    };
    MoneyGbpInputComponent.prototype.registerOnValidatorChange = function (_) {
        // Not used.
    };
    var MoneyGbpInputComponent_1;
    MoneyGbpInputComponent.PATTERN_REGEXP = new RegExp('^\\d*(\\.\\d{0,2})?$');
    __decorate([
        Input(),
        __metadata("design:type", String)
    ], MoneyGbpInputComponent.prototype, "id", void 0);
    __decorate([
        Input(),
        __metadata("design:type", String)
    ], MoneyGbpInputComponent.prototype, "name", void 0);
    __decorate([
        Input(),
        __metadata("design:type", Boolean)
    ], MoneyGbpInputComponent.prototype, "mandatory", void 0);
    __decorate([
        Input(),
        __metadata("design:type", FormControl)
    ], MoneyGbpInputComponent.prototype, "formControl", void 0);
    MoneyGbpInputComponent = MoneyGbpInputComponent_1 = __decorate([
        Component({
            selector: 'ccd-money-gbp-input',
            template: "<input class=\"form-control form-control-1-8\"\n                    type=\"text\"\n                    [id]=\"id\"\n                    [name]=\"name\"\n                    [value]=\"displayValue\"\n                    (change)=\"onChange($event)\"\n                    (keyup)=\"onChange($event)\"\n                    (blur)=\"onBlur()\"\n                    [disabled]=\"disabled\"/>",
            providers: [
                {
                    provide: NG_VALUE_ACCESSOR,
                    useExisting: forwardRef(function () { return MoneyGbpInputComponent_1; }),
                    multi: true,
                },
                {
                    provide: NG_VALIDATORS,
                    useExisting: forwardRef(function () { return MoneyGbpInputComponent_1; }),
                    multi: true,
                }
            ]
        })
    ], MoneyGbpInputComponent);
    return MoneyGbpInputComponent;
}());
export { MoneyGbpInputComponent };
//# sourceMappingURL=money-gbp-input.component.js.map