"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrosswalkAttributes = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var moment_1 = __importDefault(require("moment"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var Delete_1 = __importDefault(require("@mui/icons-material/Delete"));
var SimpleDropDownSelector_1 = require("../../../SimpleDropDownSelector");
var TextEditor_1 = require("../../../TextEditor");
var ConfirmDeleteDialog_1 = require("../../../ConfirmDeleteDialog");
var MoreAttributesButton_1 = require("../../../MoreAttributesButton");
var SmallIconButton_1 = require("../../../SmallIconButton");
var SourceIcon_1 = require("../../../SourceIcon");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var CopyableValueField_1 = require("../CopyableValueField");
var CrosswalkDateEditor_1 = require("../CrosswalkDateEditor");
var styles_1 = require("./styles");
var CrosswalkAttributes = function (_a) {
    var crosswalk = _a.crosswalk, _b = _a.canEditAttribute, canEditAttribute = _b === void 0 ? true : _b, onEditAttribute = _a.onEditAttribute, onDeleteAttribute = _a.onDeleteAttribute;
    var styles = (0, styles_1.useStyles)();
    var _c = (0, react_1.useState)([]), addedAttributes = _c[0], setAddedAttributes = _c[1];
    var _d = (0, react_1.useState)(null), pendingDeletionAttrUri = _d[0], setPendingDeletionAttrUri = _d[1];
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var isTempCrosswalk = !('uri' in crosswalk);
    var addSelectedAttributes = function (selectedAttributes) {
        if (selectedAttributes.length > 0) {
            setAddedAttributes(function (addedAttributes) { return addedAttributes.concat((0, ramda_1.map)((0, ramda_1.prop)('uri'), selectedAttributes)); });
        }
    };
    var handleDeleteAttribute = function (attributeName) {
        setAddedAttributes(function (addedAttributes) { return (0, ramda_1.without)([attributeName], addedAttributes); });
        if (!(0, ramda_1.isNil)(crosswalk[attributeName])) {
            onDeleteAttribute(attributeName);
        }
    };
    var attributes = (0, react_1.useMemo)(function () { return [
        {
            label: mdm_sdk_1.CrosswalksAttrTypes.sourceSystems.label,
            uri: 'type'
        },
        {
            label: mdm_sdk_1.CrosswalksAttrTypes.value.label,
            uri: 'value'
        },
        {
            label: mdm_sdk_1.CrosswalksAttrTypes.sourceTable.label,
            uri: 'sourceTable',
            canBeAdded: isTempCrosswalk,
            canBeDeleted: isTempCrosswalk
        },
        {
            label: mdm_sdk_1.CrosswalksAttrTypes.createDate.label,
            uri: 'createDate'
        },
        {
            label: mdm_sdk_1.CrosswalksAttrTypes.updateDate.label,
            uri: 'updateDate'
        },
        {
            label: mdm_sdk_1.CrosswalksAttrTypes.deleteDate.label,
            uri: 'deleteDate',
            canBeAdded: true,
            canBeDeleted: true
        },
        {
            label: mdm_sdk_1.CrosswalksAttrTypes.reltioLoadDate.label,
            uri: 'reltioLoadDate'
        },
        {
            label: mdm_sdk_1.CrosswalksAttrTypes.publishDate.label,
            uri: 'sourcePublishDate'
        }
    ]; }, []);
    var extraAttributes = (0, ramda_1.pipe)((0, ramda_1.filter)((0, ramda_1.propEq)('canBeAdded', true)), (0, ramda_1.filter)(function (item) { return !(0, ramda_1.has)(item.uri, crosswalk); }), (0, ramda_1.filter)(function (item) { return !addedAttributes.includes(item.uri); }))(attributes);
    var renderAttributeValue = function (attributeName, attributeValue) {
        var _a;
        switch (attributeName) {
            case 'type': {
                var sourceSystemLabel = (0, mdm_sdk_1.getSourceSystemLabel)(metadata, attributeValue);
                return isTempCrosswalk ? (react_1.default.createElement(SimpleDropDownSelector_1.SimpleDropDownSelector, { label: '', placeholder: attributeValue, className: styles.editor, value: { value: attributeValue, label: sourceSystemLabel }, options: (0, mdm_sdk_1.getCrosswalksTypes)(metadata)
                        .sort((0, ramda_1.ascend)((0, ramda_1.prop)('label')))
                        .map(function (_a) {
                        var uri = _a.uri, label = _a.label;
                        return ({
                            value: uri,
                            label: label
                        });
                    }), onChange: function (_a) {
                        var value = _a.value;
                        return onEditAttribute(attributeName, value);
                    }, TextFieldProps: {
                        InputProps: {
                            disableUnderline: true
                        },
                        inputProps: (_a = {}, _a['data-reltio-id'] = 'crosswalk-attribute-type-selector', _a)
                    } })) : (react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement(SourceIcon_1.SourceIcon, { className: styles.sourceIcon, sourceType: attributeValue }),
                    react_1.default.createElement(Typography_1.default, { className: styles.text, variant: 'body2' }, sourceSystemLabel)));
            }
            case 'sourceTable':
                return isTempCrosswalk ? (react_1.default.createElement(TextEditor_1.TextEditor, { multiple: false, multiline: false, getValueLabel: undefined, classes: { root: styles.editor }, required: true, variant: "filled", value: attributeValue, onInput: function (e) {
                        return onEditAttribute(attributeName, e.target.value);
                    }, size: "small", InputProps: {
                        disableUnderline: true,
                        classes: { input: styles.input }
                    } })) : (react_1.default.createElement(Typography_1.default, { className: styles.text, variant: 'body2' }, attributeValue));
            case 'value':
                return isTempCrosswalk ? (react_1.default.createElement(TextEditor_1.TextEditor, { multiple: false, multiline: false, getValueLabel: undefined, classes: { root: styles.editor }, required: true, variant: "filled", value: attributeValue, onInput: function (e) {
                        return onEditAttribute(attributeName, e.target.value);
                    }, size: "small", InputProps: {
                        disableUnderline: true,
                        classes: { input: styles.input }
                    } })) : (react_1.default.createElement(CopyableValueField_1.CopyableValueField, { value: attributeValue }));
            case 'deleteDate':
                return (react_1.default.createElement(CrosswalkDateEditor_1.CrosswalkDateEditor, { value: attributeValue && moment_1.default.utc((0, mdm_sdk_1.parseTimestamp)(attributeValue)).valueOf(), onChange: function (value) { return onEditAttribute(attributeName, value === null || value === void 0 ? void 0 : value.toString()); }, classes: { root: styles.editor }, disabled: !canEditAttribute }));
            case 'sourcePublishDate':
            case 'createDate':
            case 'updateDate':
            case 'reltioLoadDate':
                return (react_1.default.createElement(Typography_1.default, { className: styles.text, variant: 'body2' }, (0, mdm_sdk_1.formatDateTime)(attributeValue)));
            default:
                return null;
        }
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        extraAttributes.length > 0 && canEditAttribute && (react_1.default.createElement(MoreAttributesButton_1.MoreAttributesButton, { label: ui_i18n_1.default.text('Add attributes'), onApply: addSelectedAttributes, data: extraAttributes })),
        attributes.map(function (attribute) {
            return attribute.uri in crosswalk || addedAttributes.includes(attribute.uri) ? (react_1.default.createElement("div", { key: attribute.uri, className: styles.attributeWrapper },
                react_1.default.createElement(Typography_1.default, { variant: 'body2', className: styles.attributeTitle }, "".concat(attribute.label, ":")),
                react_1.default.createElement("div", { className: styles.attributeValue },
                    renderAttributeValue(attribute.uri, crosswalk[attribute.uri]),
                    attribute.canBeDeleted && (react_1.default.createElement(SmallIconButton_1.SmallIconButtonWithTooltip, { icon: Delete_1.default, disabled: !canEditAttribute, onClick: function () { return setPendingDeletionAttrUri(attribute.uri); }, size: "XS", tooltipTitle: ui_i18n_1.default.text('Delete attribute'), showForDisabled: true }))))) : null;
        }),
        react_1.default.createElement(ConfirmDeleteDialog_1.ConfirmDeleteDialog, { open: !!pendingDeletionAttrUri, onClose: function () { return setPendingDeletionAttrUri(null); }, onConfirm: function () { return handleDeleteAttribute(pendingDeletionAttrUri); } })));
};
exports.CrosswalkAttributes = CrosswalkAttributes;
