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 React, { useContext, useEffect, useState } from 'react';
import { filter, pipe, pluck, propEq } from 'ramda';
import { getImageAttributeOvThumbnailUrl, getImageAttributeOvUrl, isTempUri } from '@reltio/mdm-sdk';
import { ImageDetailsView } from '../ImageDetailsView';
import { ImageGalleryDialog, ImageGalleryMode } from '../ImageGalleryDialog';
import { GalleryView } from './components/GalleryView';
import { HeaderLeft } from './components/HeaderLeft';
import { HeaderRight } from './components/HeaderRight';
import { ConfigPermissionsContext } from '../contexts/ConfigPermissionsContext';
import { useMdmIsViewMode } from '../contexts/MdmModuleContext';
export var ImageAttributesGallery = function (_a) {
    var attributeValues = _a.attributeValues, open = _a.open, onClose = _a.onClose, currentAttributeValueUri = _a.currentAttributeValueUri, setCurrentAttributeValueUri = _a.setCurrentAttributeValueUri, attributeType = _a.attributeType, onDownload = _a.onDownload, onShareLink = _a.onShareLink, onSetAsDefault = _a.onSetAsDefault, onUpload = _a.onUpload, onDeleteAttribute = _a.onDeleteAttribute;
    var _b = useState([]), selectedItems = _b[0], setSelectedItems = _b[1];
    var _c = useState(ImageGalleryMode.Gallery), mode = _c[0], setMode = _c[1];
    var isViewMode = useMdmIsViewMode();
    useEffect(function () {
        setMode(currentAttributeValueUri ? ImageGalleryMode.Details : ImageGalleryMode.Gallery);
    }, [currentAttributeValueUri]);
    var imageItems = attributeValues.map(function (attribute) { return ({
        src: getImageAttributeOvThumbnailUrl(attribute),
        downloadURL: getImageAttributeOvUrl(attribute),
        id: attribute.uri
    }); });
    var handleSelect = function (id) {
        setSelectedItems(function (items) { return __spreadArray(__spreadArray([], items, true), [id], false); });
    };
    var handleDeselect = function (id) {
        setSelectedItems(function (items) { return items.filter(function (item) { return item !== id; }); });
    };
    var handleSelectAll = function () {
        setSelectedItems(pluck('id', imageItems));
    };
    var handleClearAll = function () {
        setSelectedItems([]);
    };
    var handleDownloadSelectedItems = function () {
        var downloadURLs = isGalleryMode
            ? pipe(filter(function (_a) {
                var id = _a.id;
                return selectedItems.includes(id);
            }), pluck('downloadURL'))(imageItems)
            : [imageItems.find(propEq('id', currentAttributeValueUri)).downloadURL];
        onDownload(downloadURLs);
    };
    var handleDownload = function (id) {
        onDownload([imageItems.find(propEq('id', id)).downloadURL]);
    };
    var handleShareLink = function (id) {
        onShareLink(imageItems.find(propEq('id', id)).downloadURL);
    };
    var handleSharedCurrentItem = function () {
        handleShareLink(currentAttributeValueUri);
    };
    var handleSetAsDefault = function (id) {
        var attributeValue = attributeValues.find(function (_a) {
            var uri = _a.uri;
            return uri === id;
        });
        onSetAsDefault(attributeValue);
    };
    var handleDelete = function (id) {
        onDeleteAttribute({ uri: id, attributeType: attributeType });
    };
    var handleDeleteSelectedItems = function () {
        var deleteUris = getUrisForDelete();
        deleteUris.forEach(function (uri) {
            handleDelete(uri);
        });
        if (selectedItems.length)
            setSelectedItems([]);
    };
    var _d = useContext(ConfigPermissionsContext), canCreate = _d.canCreate, canDelete = _d.canDelete;
    var isGalleryMode = mode === ImageGalleryMode.Gallery;
    var isDetailsMode = mode === ImageGalleryMode.Details;
    var hasSelectedItems = selectedItems.length > 0;
    var isHasSelectedItemsOrDetailsMode = isDetailsMode || (isGalleryMode && hasSelectedItems);
    var getUrisForDelete = function () { return (isGalleryMode ? selectedItems : [currentAttributeValueUri]); };
    var areAllItemsDeletable = getUrisForDelete().every(function (uri) { return isTempUri(uri) || canDelete; });
    var isShowDeleteButton = !isViewMode && isHasSelectedItemsOrDetailsMode && areAllItemsDeletable;
    var isShowUploadButton = !isViewMode && canCreate && (isDetailsMode || (isGalleryMode && !hasSelectedItems));
    var headerLeft = isGalleryMode && hasSelectedItems && (React.createElement(HeaderLeft, { items: imageItems, selectedItems: selectedItems, onClearAll: handleClearAll, onSelectAll: handleSelectAll }));
    var headerRight = (React.createElement(HeaderRight, { onDelete: isShowDeleteButton ? handleDeleteSelectedItems : undefined, onDownload: isHasSelectedItemsOrDetailsMode ? handleDownloadSelectedItems : undefined, onShare: isDetailsMode ? handleSharedCurrentItem : undefined, onUpload: isShowUploadButton ? onUpload : undefined }));
    var handleClickByImage = function (id) {
        setCurrentAttributeValueUri(id);
        setMode(ImageGalleryMode.Details);
    };
    return (React.createElement(ImageGalleryDialog, { mode: mode, open: open, onChangeMode: setMode, header: {
            left: headerLeft,
            right: headerRight
        }, onClose: onClose },
        isGalleryMode && (React.createElement(GalleryView, { items: imageItems, selectedItems: selectedItems, onSelectImage: handleSelect, onDeselectImage: handleDeselect, onShareLink: handleShareLink, onSetAsDefault: handleSetAsDefault, onDownload: handleDownload, onClickImage: handleClickByImage, onDelete: !isViewMode ? handleDelete : undefined })),
        isDetailsMode && (React.createElement(ImageDetailsView, { handleSetAsDefault: handleSetAsDefault, setCurrentAttributeValueUri: setCurrentAttributeValueUri, currentAttributeValueUri: currentAttributeValueUri, attributeValues: attributeValues, attributeType: attributeType }))));
};
