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));
};
import { path } from 'ramda';
import moment from 'moment';
import i18n from 'ui-i18n';
import { SYSTEM_ATTRIBUTES_TITLES } from './constants';
export 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 path([name, 0, 'value'], value);
    }
    if (name === 'CreationDate' && path(['CreationTimestamp', 0, 'value'], value)) {
        return moment(value.CreationTimestamp[0].value).format('MM-DD-YYYY');
    }
    if (name === 'CreationTime' && path(['CreationTimestamp', 0, 'value'], value)) {
        return moment(value.CreationTimestamp[0].value).format('HH:mm:ss');
    }
    if (name === 'Dimensions' && path(['Height', 0, 'value'], value) && path(['Width', 0, 'value'], value)) {
        return "".concat(value.Height[0].value, " x ").concat(value.Width[0].value, " ").concat(i18n.text('pixels'));
    }
    if (name === 'Size' && path(['Size', 0, 'value'], value)) {
        var size = (Number(value.Size[0].value) / 1000).toFixed(2);
        return "".concat(size, " KB");
    }
};
var filterByNotSystemAttributeTypes = function (attrTypes) {
    if (attrTypes === void 0) { attrTypes = []; }
    return attrTypes.filter(function (_a) {
        var system = _a.system;
        return !system;
    });
};
export 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;
    });
};
export var getMoreAttrTypes = function (attrType) {
    return filterByNotSystemAttributeTypes(attrType.attributes);
};
export var getSystemAttributesList = function (attrValue, fieldsOrder) {
    return filterByFieldsOrder(Object.keys(SYSTEM_ATTRIBUTES_TITLES), fieldsOrder)
        .map(function (key) {
        var value = getFormattedSystemImageAttributeValue(attrValue, key);
        if (!value) {
            return null;
        }
        return {
            title: SYSTEM_ATTRIBUTES_TITLES[key],
            value: getFormattedSystemImageAttributeValue(attrValue, key),
            isLink: isLink(key)
        };
    })
        .filter(Boolean);
};
export var getNotSystemAttributesList = function (attrValue, attrType, fieldsOrder) {
    if (attrValue === void 0) { attrValue = {}; }
    var notSystemAttrTypes = filterByNotSystemAttributeTypes(attrType.attributes);
    return 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;
    }, []);
};
export var isLink = function (key) {
    return ['Url', 'UrlPreview', 'UrlThumbnail', 'CdnUrl', 'CdnUrlPreview', 'CdnUrlThumbnail'].includes(key);
};
