"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;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
var _utils = require("../Attach/utils");
var _getFileIcon = require("./utils/getFileIcon");
var _base = require("./variations/_view/base");
var _base1 = require("./variations/_size/base");
var _base2 = require("./variations/_disabled/base");
var _Filestyles = require("./File.styles");
var _Filetokens = require("./File.tokens");
var _ui = require("./ui");
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 _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;
}
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;
}
var fileRoot = function fileRoot(Root) {
    return /*#__PURE__*/ (0, _react.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 = (0, _react.useMemo)(function() {
            var ext = filename.match(/\./) ? (0, _utils.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.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;
        };
        var renderFileThumb = function 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, _object_spread({
            ref: ref,
            className: (0, _classnames.default)(className, _Filetokens.classes.fileItem, _define_property({}, _Filetokens.classes.fileLoaderLinear, loaderType === 'linear')),
            view: view,
            size: size,
            disabled: disabled
        }, rest), /*#__PURE__*/ _react.default.createElement(_ui.CellUI, {
            className: (0, _classnames.default)(_define_property({}, _Filetokens.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.default.createElement(_react.default.Fragment, null, loader || /*#__PURE__*/ _react.default.createElement(_ui.ProgressUI, {
            displayValue: false,
            value: loaderValue
        })));
    });
};
var 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'
    }
};
