"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLink = exports.getNotSystemAttributesList = exports.getSystemAttributesList = exports.getMoreAttrTypes = exports.filterByFieldsOrder = exports.getFormattedSystemImageAttributeValue = void 0;
var ramda_1 = require("ramda");
var moment_1 = __importDefault(require("moment"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var constants_1 = require("./constants");
var getFormattedSystemImageAttributeValue = function (_a, name) {
    var _b = _a === void 0 ? {} : _a, value = _b.value;
    if (['Url', 'UrlPreview', 'UrlThumbnail', 'CdnUrl', 'CdnUrlPreview', 'CdnUrlThumbnail', 'MimeType'].includes(name)) {
        return (0, ramda_1.path)([name, 0, 'value'], value);
    }
    if (name === 'CreationDate' && (0, ramda_1.path)(['CreationTimestamp', 0, 'value'], value)) {
        return (0, moment_1.default)(value.CreationTimestamp[0].value).format('MM-DD-YYYY');
    }
    if (name === 'CreationTime' && (0, ramda_1.path)(['CreationTimestamp', 0, 'value'], value)) {
        return (0, moment_1.default)(value.CreationTimestamp[0].value).format('HH:mm:ss');
    }
    if (name === 'Dimensions' && (0, ramda_1.path)(['Height', 0, 'value'], value) && (0, ramda_1.path)(['Width', 0, 'value'], value)) {
        return "".concat(value.Height[0].value, " x ").concat(value.Width[0].value, " ").concat(ui_i18n_1.default.text('pixels'));
    }
    if (name === 'Size' && (0, ramda_1.path)(['Size', 0, 'value'], value)) {
        var size = (Number(value.Size[0].value) / 1000).toFixed(2);
        return "".concat(size, " KB");
    }
};
exports.getFormattedSystemImageAttributeValue = getFormattedSystemImageAttributeValue;
var filterByNotSystemAttributeTypes = function (attrTypes) {
    if (attrTypes === void 0) { attrTypes = []; }
    return attrTypes.filter(function (_a) {
        var system = _a.system;
        return !system;
    });
};
var filterByFieldsOrder = function (keys, fieldsOrder) {
    var _a = fieldsOrder.includeNames, includeNames = _a === void 0 ? [] : _a, _b = fieldsOrder.excludeNames, excludeNames = _b === void 0 ? [] : _b, _c = fieldsOrder.order, order = _c === void 0 ? [] : _c;
    var filteredKeys = includeNames.length
        ? keys.filter(function (key) { return includeNames.includes(key); })
        : keys.filter(function (key) { return !excludeNames.includes(key); });
    if (order.length === 0) {
        return filteredKeys;
    }
    return filteredKeys.sort(function (a, b) {
        var indA = order.indexOf(a);
        var indB = order.indexOf(b);
        if (indA === -1 && indB !== -1)
            return 1;
        if (indB === -1 && indA !== -1)
            return -1;
        return indA - indB;
    });
};
exports.filterByFieldsOrder = filterByFieldsOrder;
var getMoreAttrTypes = function (attrType) {
    return filterByNotSystemAttributeTypes(attrType.attributes);
};
exports.getMoreAttrTypes = getMoreAttrTypes;
var getSystemAttributesList = function (attrValue, fieldsOrder) {
    return (0, exports.filterByFieldsOrder)(Object.keys(constants_1.SYSTEM_ATTRIBUTES_TITLES), fieldsOrder)
        .map(function (key) {
        var value = (0, exports.getFormattedSystemImageAttributeValue)(attrValue, key);
        if (!value) {
            return null;
        }
        return {
            title: constants_1.SYSTEM_ATTRIBUTES_TITLES[key],
            value: (0, exports.getFormattedSystemImageAttributeValue)(attrValue, key),
            isLink: (0, exports.isLink)(key)
        };
    })
        .filter(Boolean);
};
exports.getSystemAttributesList = getSystemAttributesList;
var getNotSystemAttributesList = function (attrValue, attrType, fieldsOrder) {
    if (attrValue === void 0) { attrValue = {}; }
    var notSystemAttrTypes = filterByNotSystemAttributeTypes(attrType.attributes);
    return (0, exports.filterByFieldsOrder)(Object.keys(attrValue.value || {}), fieldsOrder).reduce(function (result, key) {
        var attrType = notSystemAttrTypes.find(function (_a) {
            var name = _a.name;
            return name === key;
        });
        return attrType ? __spreadArray(__spreadArray([], result, true), [{ attrType: attrType, values: attrValue.value[key] }], false) : result;
    }, []);
};
exports.getNotSystemAttributesList = getNotSystemAttributesList;
var isLink = function (key) {
    return ['Url', 'UrlPreview', 'UrlThumbnail', 'CdnUrl', 'CdnUrlPreview', 'CdnUrlThumbnail'].includes(key);
};
exports.isLink = isLink;
