"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
/*
  The MIT License

  Copyright (c) 2017-2019 EclipseSource Munich
  https://github.com/eclipsesource/jsonforms

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
var core_1 = require("@jsonforms/core");
var core_2 = require("@angular/core");
var forms_1 = require("@angular/forms");
var base_renderer_1 = require("./base.renderer");
var merge_1 = tslib_1.__importDefault(require("lodash/merge"));
var JsonFormsAbstractControl = /** @class */ (function (_super) {
    tslib_1.__extends(JsonFormsAbstractControl, _super);
    function JsonFormsAbstractControl(jsonFormsService) {
        var _this = _super.call(this) || this;
        _this.jsonFormsService = jsonFormsService;
        _this.getEventValue = function (event) { return event.value; };
        _this.validator = function (_c) {
            return _this.error ? { error: _this.error } : null;
        };
        _this.form = new forms_1.FormControl({
            value: '',
            disabled: true
        }, {
            updateOn: 'change',
            validators: _this.validator.bind(_this)
        });
        return _this;
    }
    JsonFormsAbstractControl.prototype.onChange = function (ev) {
        var _this = this;
        this.jsonFormsService.updateCore(core_1.Actions.update(this.propsPath, function () { return _this.getEventValue(ev); }));
        this.triggerValidation();
    };
    JsonFormsAbstractControl.prototype.shouldShowUnfocusedDescription = function () {
        var config = this.jsonFormsService.getState().jsonforms.config;
        var appliedUiSchemaOptions = merge_1.default({}, config, this.uischema.options);
        return !!appliedUiSchemaOptions.showUnfocusedDescription;
    };
    JsonFormsAbstractControl.prototype.ngOnInit = function () {
        var _this = this;
        this.jsonFormsService.$state.subscribe({
            next: function (state) {
                var props = _this.mapToProps(state);
                var data = props.data, enabled = props.enabled, errors = props.errors, label = props.label, required = props.required, schema = props.schema, rootSchema = props.rootSchema, visible = props.visible, path = props.path, config = props.config;
                _this.label = core_1.computeLabel(core_1.isPlainLabel(label) ? label : label.default, required, config ? config.hideRequiredAsterisk : false);
                _this.data = data;
                _this.error = errors;
                _this.enabled = enabled;
                _this.isEnabled() ? _this.form.enable() : _this.form.disable();
                _this.hidden = !visible;
                _this.scopedSchema = schema;
                _this.rootSchema = rootSchema;
                _this.description =
                    _this.scopedSchema !== undefined ? _this.scopedSchema.description : '';
                _this.id = props.id;
                _this.form.setValue(data);
                _this.propsPath = path;
                _this.mapAdditionalProps(props);
            }
        });
        this.triggerValidation();
    };
    // @ts-ignore
    JsonFormsAbstractControl.prototype.mapAdditionalProps = function (props) {
        // do nothing by default
    };
    JsonFormsAbstractControl.prototype.ngOnDestroy = function () {
        if (this.subscription) {
            this.subscription.unsubscribe();
        }
    };
    JsonFormsAbstractControl.prototype.isEnabled = function () {
        return (this.enabled &&
            (this.uischema.options ? !this.uischema.options.readonly : true));
    };
    JsonFormsAbstractControl.prototype.getOwnProps = function () {
        var props = {
            uischema: this.uischema,
            schema: this.schema,
            path: this.path,
            id: this.id
        };
        if (this.disabled !== undefined) {
            props.enabled = !this.disabled;
        }
        if (this.visible !== undefined) {
            props.visible = this.visible;
        }
        return props;
    };
    JsonFormsAbstractControl.prototype.triggerValidation = function () {
        // these cause the correct update of the error underline, seems to be
        // related to ionic-team/ionic#11640
        this.form.markAsTouched();
        this.form.updateValueAndValidity();
    };
    tslib_1.__decorate([
        core_2.Input(),
        tslib_1.__metadata("design:type", String)
    ], JsonFormsAbstractControl.prototype, "id", void 0);
    tslib_1.__decorate([
        core_2.Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], JsonFormsAbstractControl.prototype, "disabled", void 0);
    tslib_1.__decorate([
        core_2.Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], JsonFormsAbstractControl.prototype, "visible", void 0);
    return JsonFormsAbstractControl;
}(base_renderer_1.JsonFormsBaseRenderer));
exports.JsonFormsAbstractControl = JsonFormsAbstractControl;
//# sourceMappingURL=abstract-control.js.map