"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var debounce = require("lodash/debounce");
var react_1 = require("@storybook/react");
var storybook_chrome_screenshot_1 = require("storybook-chrome-screenshot");
var _1 = require("../../");
var Ajv = require('ajv');
var ajvKeywords = require('ajv-keywords');
var metaSchema6 = require('ajv/lib/refs/json-schema-draft-06.json');
var ajv = new Ajv();
ajvKeywords(ajv);
ajv.addMetaSchema(metaSchema6);
window.ajv = ajv;
var Validator = (function (_super) {
    __extends(Validator, _super);
    function Validator() {
        var _this = _super.call(this) || this;
        _this.state = {
            data: '',
            schema: '',
            schemaError: null,
            errors: null,
            valid: null
        };
        _this.validate = debounce(function () {
            _this.setState({ errors: null, valid: null });
            try {
                var schema = JSON.parse(_this.state.schema);
                ajv.removeSchema(/^.*$/);
                var validate = void 0;
                try {
                    validate = ajv.compile(schema);
                    _this.setState({ schemaError: null });
                }
                catch (e) {
                    _this.setState({ schemaError: e.message });
                    return;
                }
                var data = JSON.parse(_this.state.data);
                var valid = validate(data);
                if (valid) {
                    _this.setState({ valid: true });
                }
                else {
                    _this.setState({ errors: validate.errors });
                }
            }
            catch (e) {
                console.error(e);
            }
        }, 700);
        return _this;
    }
    Validator.prototype.changeSchema = function (schema) {
        var _this = this;
        this.setState({ schema: schema }, function () { return _this.validate(); });
    };
    Validator.prototype.changeData = function (data) {
        var _this = this;
        this.setState({ data: data }, function () { return _this.validate(); });
    };
    Validator.prototype.render = function () {
        var _this = this;
        return (React.createElement(_1.Box, null,
            React.createElement(_1.Flex, { p: 3 },
                React.createElement(_1.Box, { p: 2, flex: '1' },
                    React.createElement("label", null, "Schema"),
                    React.createElement(_1.Textarea, { monospace: true, rows: '10', onChange: function (e) { return _this.changeSchema(e.target.value); }, value: this.state.schema })),
                React.createElement(_1.Box, { p: 2, flex: '1' },
                    React.createElement("label", null, "JSON"),
                    React.createElement(_1.Textarea, { monospace: true, rows: '10', onChange: function (e) { return _this.changeData(e.target.value); }, value: this.state.data }))),
            this.state.schemaError && (React.createElement(_1.Box, { p: 3 },
                React.createElement(_1.Txt, { color: 'red' }, this.state.schemaError))),
            React.createElement(_1.Box, { p: 3 },
                this.state.valid && React.createElement(_1.Txt, { color: 'green' }, "No errors found"),
                this.state.errors &&
                    this.state.errors.map(function (err, index) { return (React.createElement(_1.Txt, { key: index, color: 'red' },
                        React.createElement("strong", null, err.schemaPath),
                        ": ",
                        err.message)); }))));
    };
    return Validator;
}(React.Component));
react_1.storiesOf('Tools/JSON Schema Validator', module)
    .addDecorator(storybook_chrome_screenshot_1.withScreenshot())
    .add('Draft 6', function () {
    return (React.createElement(_1.Provider, null,
        React.createElement(Validator, null)));
});
