import { __assign, __extends, __spreadArray } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import AbstractUploadList from '../AbstractList';
import NormalUploadItem from './Item';
import MiniPagination from '../../../pagination/MiniPagination';
import Sortable from '../../../sortable';
var NormalUploadList = (function (_super) {
    __extends(NormalUploadList, _super);
    function NormalUploadList() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            current: 1,
        };
        _this.onFileListSortChange = function (list) {
            var _a = _this.displayListRange, start = _a[0], end = _a[1];
            var rawFileList = _this.props.fileList;
            var newList = __spreadArray(__spreadArray(__spreadArray([], rawFileList.slice(0, start)), list), rawFileList.slice(end));
            _this.props.onSortChange(newList);
        };
        _this.onPaginationChange = function (pageInfo) {
            _this.setState({
                current: pageInfo.current,
            });
        };
        _this.renderFileItem = function (item) {
            var CustomUploadItem = _this.props.customUploadItem;
            var UploadItem = CustomUploadItem || NormalUploadItem;
            return (_jsx(UploadItem, { item: item, i18n: _this.props.i18n, onDelete: _this.props.onDelete, onRetry: _this.props.onRetry }, item.id));
        };
        return _this;
    }
    NormalUploadList.prototype.componentDidUpdate = function (prevProps) {
        var prevFileList = prevProps.fileList, prevPageSize = prevProps.pageSize;
        var _a = this.props, fileList = _a.fileList, pageSize = _a.pageSize;
        var current = this.state.current;
        if (fileList.length < prevFileList.length || prevPageSize !== pageSize) {
            var maxPage = Math.max(Math.ceil(fileList.length / pageSize), 1);
            if (maxPage < current) {
                this.setState({
                    current: maxPage,
                });
            }
        }
    };
    Object.defineProperty(NormalUploadList.prototype, "displayListRange", {
        get: function () {
            if (!this.props.pagination) {
                return [0, this.props.fileList.length];
            }
            var current = this.state.current;
            var pageSize = this.props.pageSize;
            return [(current - 1) * pageSize, current * pageSize];
        },
        enumerable: false,
        configurable: true
    });
    NormalUploadList.prototype.getRenderFileList = function () {
        var _a = this.displayListRange, start = _a[0], end = _a[1];
        return this.props.fileList.slice(start, end);
    };
    NormalUploadList.prototype.renderPagination = function () {
        if (!this.props.pagination) {
            return null;
        }
        var current = this.state.current;
        var pageSize = this.props.pageSize;
        return (_jsx(MiniPagination, { className: "zent-file-upload-list-pagination", onChange: this.onPaginationChange, current: current, pageSize: pageSize, total: this.props.fileList.length }, void 0));
    };
    NormalUploadList.prototype.render = function () {
        var sortable = this.props.sortable;
        var fileList = this.getRenderFileList();
        if (!fileList || !fileList.length) {
            return null;
        }
        var fileListContent = fileList.map(this.renderFileItem);
        var listContent = sortable ? (_jsx(Sortable, __assign({ tag: "ul", items: fileList, className: "zent-file-upload-list", onChange: this.onFileListSortChange }, { children: fileListContent }), void 0)) : (_jsx("ul", __assign({ className: "zent-file-upload-list", "data-zv": 'v10.0.11' }, { children: fileListContent }), void 0));
        return (_jsxs("div", __assign({ className: "zent-file-upload-list-wrapper", "data-zv": 'v10.0.11' }, { children: [listContent, this.renderPagination()] }), void 0));
    };
    return NormalUploadList;
}(AbstractUploadList));
export default NormalUploadList;
