"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DCRValueEditorFactory = void 0;
var ramda_1 = require("ramda");
var react_1 = __importDefault(require("react"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var DataTypeValueEditor_1 = require("../../../../../DataTypeValueEditor");
var attributesView_1 = require("../../../../../helpers/attributesView");
var RolesInput_1 = require("../../../../../RolesInput/RolesInput");
var TagsInput_1 = require("../../../../../TagsInput/TagsInput");
var common_1 = require("../../../helpers/common");
var DCRValueEditorFactory = /** @class */ (function () {
    function DCRValueEditorFactory() {
    }
    DCRValueEditorFactory.build = function (_a) {
        var attributeType = _a.attributeType, value = _a.value, onChange = _a.onChange, _b = _a.disabled, disabled = _b === void 0 ? false : _b, _c = _a.disableTimestampLocalization, disableTimestampLocalization = _c === void 0 ? false : _c;
        var dcrValue = (0, mdm_sdk_1.getAttributeValue)(value);
        switch (attributeType.uri) {
            case mdm_sdk_1.EntityAttrTypes.roles.uri: {
                var handleChange = function (values) {
                    onChange((values === null || values === void 0 ? void 0 : values.map((0, ramda_1.prop)('value'))) || []);
                };
                return (react_1.default.createElement(RolesInput_1.RolesInput, { value: dcrValue ? (0, mdm_sdk_1.wrapInArrayIfNeeded)(dcrValue) : [], onChange: handleChange, disabled: disabled }));
            }
            case mdm_sdk_1.EntityAttrTypes.tags.uri: {
                return (react_1.default.createElement(TagsInput_1.TagsInput, { value: dcrValue ? (0, mdm_sdk_1.wrapInArrayIfNeeded)(dcrValue) : [], onChange: onChange, disabled: disabled }));
            }
            default: {
                var dataTypeDefinition = (0, common_1.getDataTypeDefinition)(attributeType, dcrValue);
                var editorValue = value && dataTypeDefinition
                    ? (0, attributesView_1.attributeValueToEditorValue)(value, dataTypeDefinition, {
                        disableTimestampLocalization: disableTimestampLocalization
                    })
                    : null;
                var handleChange = function (newValue) {
                    var valueConvertedToAttributeValue = (0, attributesView_1.editorValueToAttributeValue)(attributeType, newValue, {
                        disableTimestampLocalization: disableTimestampLocalization
                    });
                    onChange(valueConvertedToAttributeValue);
                };
                return (react_1.default.createElement(DataTypeValueEditor_1.DataTypeValueEditor, { fieldName: attributeType.uri, value: editorValue, dataTypeDefinition: dataTypeDefinition, onChange: handleChange, fullWidth: true, disabled: disabled }));
            }
        }
    };
    return DCRValueEditorFactory;
}());
exports.DCRValueEditorFactory = DCRValueEditorFactory;
