"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 __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));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageAttributesGallery = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ImageDetailsView_1 = require("../ImageDetailsView");
var ImageGalleryDialog_1 = require("../ImageGalleryDialog");
var GalleryView_1 = require("./components/GalleryView");
var HeaderLeft_1 = require("./components/HeaderLeft");
var HeaderRight_1 = require("./components/HeaderRight");
var ConfigPermissionsContext_1 = require("../contexts/ConfigPermissionsContext");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
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 = (0, react_1.useState)([]), selectedItems = _b[0], setSelectedItems = _b[1];
    var _c = (0, react_1.useState)(ImageGalleryDialog_1.ImageGalleryMode.Gallery), mode = _c[0], setMode = _c[1];
    var isViewMode = (0, MdmModuleContext_1.useMdmIsViewMode)();
    (0, react_1.useEffect)(function () {
        setMode(currentAttributeValueUri ? ImageGalleryDialog_1.ImageGalleryMode.Details : ImageGalleryDialog_1.ImageGalleryMode.Gallery);
    }, [currentAttributeValueUri]);
    var imageItems = attributeValues.map(function (attribute) { return ({
        src: (0, mdm_sdk_1.getImageAttributeOvThumbnailUrl)(attribute),
        downloadURL: (0, mdm_sdk_1.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((0, ramda_1.pluck)('id', imageItems));
    };
    var handleClearAll = function () {
        setSelectedItems([]);
    };
    var handleDownloadSelectedItems = function () {
        var downloadURLs = isGalleryMode
            ? (0, ramda_1.pipe)((0, ramda_1.filter)(function (_a) {
                var id = _a.id;
                return selectedItems.includes(id);
            }), (0, ramda_1.pluck)('downloadURL'))(imageItems)
            : [imageItems.find((0, ramda_1.propEq)('id', currentAttributeValueUri)).downloadURL];
        onDownload(downloadURLs);
    };
    var handleDownload = function (id) {
        onDownload([imageItems.find((0, ramda_1.propEq)('id', id)).downloadURL]);
    };
    var handleShareLink = function (id) {
        onShareLink(imageItems.find((0, ramda_1.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 = (0, react_1.useContext)(ConfigPermissionsContext_1.ConfigPermissionsContext), canCreate = _d.canCreate, canDelete = _d.canDelete;
    var isGalleryMode = mode === ImageGalleryDialog_1.ImageGalleryMode.Gallery;
    var isDetailsMode = mode === ImageGalleryDialog_1.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 (0, mdm_sdk_1.isTempUri)(uri) || canDelete; });
    var isShowDeleteButton = !isViewMode && isHasSelectedItemsOrDetailsMode && areAllItemsDeletable;
    var isShowUploadButton = !isViewMode && canCreate && (isDetailsMode || (isGalleryMode && !hasSelectedItems));
    var headerLeft = isGalleryMode && hasSelectedItems && (react_1.default.createElement(HeaderLeft_1.HeaderLeft, { items: imageItems, selectedItems: selectedItems, onClearAll: handleClearAll, onSelectAll: handleSelectAll }));
    var headerRight = (react_1.default.createElement(HeaderRight_1.HeaderRight, { onDelete: isShowDeleteButton ? handleDeleteSelectedItems : undefined, onDownload: isHasSelectedItemsOrDetailsMode ? handleDownloadSelectedItems : undefined, onShare: isDetailsMode ? handleSharedCurrentItem : undefined, onUpload: isShowUploadButton ? onUpload : undefined }));
    var handleClickByImage = function (id) {
        setCurrentAttributeValueUri(id);
        setMode(ImageGalleryDialog_1.ImageGalleryMode.Details);
    };
    return (react_1.default.createElement(ImageGalleryDialog_1.ImageGalleryDialog, { mode: mode, open: open, onChangeMode: setMode, header: {
            left: headerLeft,
            right: headerRight
        }, onClose: onClose },
        isGalleryMode && (react_1.default.createElement(GalleryView_1.GalleryView, { items: imageItems, selectedItems: selectedItems, onSelectImage: handleSelect, onDeselectImage: handleDeselect, onShareLink: handleShareLink, onSetAsDefault: handleSetAsDefault, onDownload: handleDownload, onClickImage: handleClickByImage, onDelete: !isViewMode ? handleDelete : undefined })),
        isDetailsMode && (react_1.default.createElement(ImageDetailsView_1.ImageDetailsView, { handleSetAsDefault: handleSetAsDefault, setCurrentAttributeValueUri: setCurrentAttributeValueUri, currentAttributeValueUri: currentAttributeValueUri, attributeValues: attributeValues, attributeType: attributeType }))));
};
exports.ImageAttributesGallery = ImageAttributesGallery;
