"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get fileConfig () {
        return fileConfig;
    },
    get fileRoot () {
        return fileRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _utils = require("../Attach/utils");
const _getFileIcon = require("./utils/getFileIcon");
const _base = require("./variations/_view/base");
const _base1 = require("./variations/_size/base");
const _base2 = require("./variations/_disabled/base");
const _Filestyles = require("./File.styles");
const _Filetokens = require("./File.tokens");
const _ui = require("./ui");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const fileRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)((props, ref)=>{
        const { className, // layout
        filename, description, actionContent, actionPlacement = 'right', fileUrl, thumbUrl, thumbIcon, // loader
        loader, isLoading = false, loaderType = 'circular', loaderValue = 0, loaderMaxValue = 100, // variations
        view, size, disabled = false, // events
        onCancel, onActionClick, ...rest } = props;
        const { extension, filenameWithoutExtension } = (0, _react.useMemo)(()=>{
            const ext = filename.match(/\./) ? (0, _utils.extractExtension)(filename) : '';
            const name = ext ? filename.slice(0, -1 - ext.length) : filename;
            return {
                extension: ext || '',
                filenameWithoutExtension: name
            };
        }, [
            filename
        ]);
        const renderAction = ()=>{
            if (isLoading && loaderType === 'circular') {
                return loader || /*#__PURE__*/ _react.default.createElement(_ui.ProgressBarCircularUI, {
                    value: loaderValue,
                    maxValue: loaderMaxValue
                }, /*#__PURE__*/ _react.default.createElement(_ui.EmbedIconButtonUI, {
                    onClick: onCancel,
                    type: "button",
                    "aria-label": "Cancel"
                }, actionContent));
            }
            if (actionContent) {
                return /*#__PURE__*/ _react.default.createElement(_ui.EmbedIconButtonUI, {
                    onClick: onActionClick
                }, actionContent);
            }
            return null;
        };
        const renderFileThumb = ()=>{
            if (actionPlacement === 'left') {
                return renderAction();
            }
            if (thumbUrl) {
                return /*#__PURE__*/ _react.default.createElement(_Filestyles.FileThumb, null, /*#__PURE__*/ _react.default.createElement(_Filestyles.ThumbImg, {
                    src: thumbUrl,
                    alt: ""
                }));
            }
            if (thumbIcon) {
                return /*#__PURE__*/ _react.default.createElement(_Filestyles.FileThumb, null, thumbIcon);
            }
            if (fileUrl) {
                return /*#__PURE__*/ _react.default.createElement(_Filestyles.FileThumb, null, (0, _getFileIcon.getFileIcon)(extension, size));
            }
            return null;
        };
        return /*#__PURE__*/ _react.default.createElement(Root, {
            ref: ref,
            className: (0, _classnames.default)(className, _Filetokens.classes.fileItem, {
                [_Filetokens.classes.fileLoaderLinear]: loaderType === 'linear'
            }),
            view: view,
            size: size,
            disabled: disabled,
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(_ui.CellUI, {
            className: (0, _classnames.default)({
                [_Filetokens.classes.fileActionLeft]: actionPlacement === 'left'
            }),
            filenameWithoutExtension: filenameWithoutExtension,
            extension: extension ? `.${extension}` : '',
            description: description,
            size: size,
            cellContentLeft: renderFileThumb(),
            cellContentRight: actionPlacement === 'right' ? renderAction() : null
        }), isLoading && loaderType === 'linear' && /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, loader || /*#__PURE__*/ _react.default.createElement(_ui.ProgressUI, {
            displayValue: false,
            value: loaderValue
        })));
    });
const fileConfig = {
    name: 'File',
    tag: 'div',
    layout: fileRoot,
    base: _Filestyles.base,
    variations: {
        view: {
            css: _base.base
        },
        size: {
            css: _base1.base
        },
        disabled: {
            css: _base2.base,
            attrs: true
        }
    },
    defaults: {
        view: 'default',
        size: 'm'
    }
};
