"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JSONFieldRenderer = exports.JSONField = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var factory_1 = require("../factory");
var react_json_view_1 = tslib_1.__importStar(require("react-json-view"));
var helper_1 = require("../utils/helper");
var tpl_builtin_1 = require("../utils/tpl-builtin");
var JSONField = /** @class */ (function (_super) {
    tslib_1.__extends(JSONField, _super);
    function JSONField() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    JSONField.prototype.emitChange = function (e) {
        var _a = this.props, onChange = _a.onChange, name = _a.name;
        if (!name || !onChange) {
            return false;
        }
        onChange(e.updated_src, name);
        return true;
    };
    JSONField.prototype.shouldExpandNode = function (_a) {
        var namespace = _a.namespace;
        var levelExpand = this.props.levelExpand;
        if (typeof levelExpand !== 'number') {
            return false;
        }
        return namespace.length > levelExpand;
    };
    JSONField.prototype.render = function () {
        var _a;
        var _b = this.props, className = _b.className, jsonTheme = _b.jsonTheme, cx = _b.classnames, placeholder = _b.placeholder, source = _b.source, levelExpand = _b.levelExpand, mutable = _b.mutable, name = _b.name;
        var value = helper_1.getPropValue(this.props);
        var data = value;
        if (source !== undefined && tpl_builtin_1.isPureVariable(source)) {
            data = tpl_builtin_1.resolveVariableAndFilter(source, this.props.data, '| raw');
        }
        else if (typeof value === 'string') {
            try {
                data = JSON.parse(value);
            }
            catch (e) {
                data = {
                    error: e.message
                };
            }
        }
        return (react_1.default.createElement("div", { className: cx('JsonField', className) }, typeof data === 'undefined' || data === null ? (placeholder) : (react_1.default.createElement(react_json_view_1.default, { name: false, src: data, theme: (_a = jsonTheme) !== null && _a !== void 0 ? _a : 'rjv-default', shouldCollapse: this.shouldExpandNode, enableClipboard: false, iconStyle: "square", onEdit: name && mutable ? this.emitChange : false, onDelete: name && mutable ? this.emitChange : false, onAdd: name && mutable ? this.emitChange : false }))));
    };
    var _a;
    JSONField.defaultProps = {
        placeholder: '-',
        levelExpand: 1,
        source: ''
    };
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [typeof (_a = typeof react_json_view_1.InteractionProps !== "undefined" && react_json_view_1.InteractionProps) === "function" ? _a : Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], JSONField.prototype, "emitChange", null);
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", [Object]),
        tslib_1.__metadata("design:returntype", void 0)
    ], JSONField.prototype, "shouldExpandNode", null);
    return JSONField;
}(react_1.default.Component));
exports.JSONField = JSONField;
var JSONFieldRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(JSONFieldRenderer, _super);
    function JSONFieldRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    JSONFieldRenderer = tslib_1.__decorate([
        factory_1.Renderer({
            type: 'json'
        })
    ], JSONFieldRenderer);
    return JSONFieldRenderer;
}(JSONField));
exports.JSONFieldRenderer = JSONFieldRenderer;
//# sourceMappingURL=./renderers/Json.js.map
