function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
import React, { forwardRef, useMemo } from "react";
import cls from "classnames";
import { extractExtension } from "../Attach/utils";
import { getFileIcon } from "./utils/getFileIcon";
import { base as viewCSS } from "./variations/_view/base";
import { base as sizeCSS } from "./variations/_size/base";
import { base as disabledCSS } from "./variations/_disabled/base";
import { base, FileThumb, ThumbImg } from "./File.styles";
import { classes } from "./File.tokens";
import { CellUI, EmbedIconButtonUI, ProgressBarCircularUI, ProgressUI } from "./ui";
export var fileRoot = function fileRoot(Root) {
    return /*#__PURE__*/ forwardRef(function(props, ref) {
        var className = props.className, // layout
        filename = props.filename, description = props.description, actionContent = props.actionContent, _props_actionPlacement = props.actionPlacement, actionPlacement = _props_actionPlacement === void 0 ? 'right' : _props_actionPlacement, fileUrl = props.fileUrl, thumbUrl = props.thumbUrl, thumbIcon = props.thumbIcon, // loader
        loader = props.loader, _props_isLoading = props.isLoading, isLoading = _props_isLoading === void 0 ? false : _props_isLoading, _props_loaderType = props.loaderType, loaderType = _props_loaderType === void 0 ? 'circular' : _props_loaderType, _props_loaderValue = props.loaderValue, loaderValue = _props_loaderValue === void 0 ? 0 : _props_loaderValue, _props_loaderMaxValue = props.loaderMaxValue, loaderMaxValue = _props_loaderMaxValue === void 0 ? 100 : _props_loaderMaxValue, // variations
        view = props.view, size = props.size, _props_disabled = props.disabled, disabled = _props_disabled === void 0 ? false : _props_disabled, // events
        onCancel = props.onCancel, onActionClick = props.onActionClick, rest = _object_without_properties(props, [
            "className",
            "filename",
            "description",
            "actionContent",
            "actionPlacement",
            "fileUrl",
            "thumbUrl",
            "thumbIcon",
            "loader",
            "isLoading",
            "loaderType",
            "loaderValue",
            "loaderMaxValue",
            "view",
            "size",
            "disabled",
            "onCancel",
            "onActionClick"
        ]);
        var _useMemo = useMemo(function() {
            var ext = filename.match(/\./) ? extractExtension(filename) : '';
            var name = ext ? filename.slice(0, -1 - ext.length) : filename;
            return {
                extension: ext || '',
                filenameWithoutExtension: name
            };
        }, [
            filename
        ]), extension = _useMemo.extension, filenameWithoutExtension = _useMemo.filenameWithoutExtension;
        var renderAction = function renderAction() {
            if (isLoading && loaderType === 'circular') {
                return loader || /*#__PURE__*/ React.createElement(ProgressBarCircularUI, {
                    value: loaderValue,
                    maxValue: loaderMaxValue
                }, /*#__PURE__*/ React.createElement(EmbedIconButtonUI, {
                    onClick: onCancel,
                    type: "button",
                    "aria-label": "Cancel"
                }, actionContent));
            }
            if (actionContent) {
                return /*#__PURE__*/ React.createElement(EmbedIconButtonUI, {
                    onClick: onActionClick
                }, actionContent);
            }
            return null;
        };
        var renderFileThumb = function renderFileThumb() {
            if (actionPlacement === 'left') {
                return renderAction();
            }
            if (thumbUrl) {
                return /*#__PURE__*/ React.createElement(FileThumb, null, /*#__PURE__*/ React.createElement(ThumbImg, {
                    src: thumbUrl,
                    alt: ""
                }));
            }
            if (thumbIcon) {
                return /*#__PURE__*/ React.createElement(FileThumb, null, thumbIcon);
            }
            if (fileUrl) {
                return /*#__PURE__*/ React.createElement(FileThumb, null, getFileIcon(extension, size));
            }
            return null;
        };
        return /*#__PURE__*/ React.createElement(Root, _object_spread({
            ref: ref,
            className: cls(className, classes.fileItem, _define_property({}, classes.fileLoaderLinear, loaderType === 'linear')),
            view: view,
            size: size,
            disabled: disabled
        }, rest), /*#__PURE__*/ React.createElement(CellUI, {
            className: cls(_define_property({}, classes.fileActionLeft, actionPlacement === 'left')),
            filenameWithoutExtension: filenameWithoutExtension,
            extension: extension ? ".".concat(extension) : '',
            description: description,
            size: size,
            cellContentLeft: renderFileThumb(),
            cellContentRight: actionPlacement === 'right' ? renderAction() : null
        }), isLoading && loaderType === 'linear' && /*#__PURE__*/ React.createElement(React.Fragment, null, loader || /*#__PURE__*/ React.createElement(ProgressUI, {
            displayValue: false,
            value: loaderValue
        })));
    });
};
export var fileConfig = {
    name: 'File',
    tag: 'div',
    layout: fileRoot,
    base: base,
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        },
        disabled: {
            css: disabledCSS,
            attrs: true
        }
    },
    defaults: {
        view: 'default',
        size: 'm'
    }
};
