"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const Container = styled_components_1.default.div `
  .ant-upload-list-picture-card-container {
    width: 226px;
  }
  .ant-upload-list-item-list-type-picture-card {
    width: 226px !important;
  }
  .ant-upload-select-picture-card {
    width: 226px !important;
  }
`;
function getBase64(file) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result);
        reader.onerror = (error) => reject(error);
    });
}
function CoverUpload(props) {
    const [previewVisible, setPreviewVisible] = (0, react_1.useState)(false);
    const [previewImage, setPreviewImage] = (0, react_1.useState)('');
    const [fileList, setFileList] = (0, react_1.useState)(props.value && props.value.url ? [{ uid: props.value.id, id: props.value.id, url: props.value.url }] : []);
    const { value, onChange } = props;
    const uploadProps = {
        name: 'file',
        // @ts-ignore
        action: `${APP_CONFIG.foxpageApi}api/picture/upload`,
        headers: {},
        withCredentials: true,
        accept: 'image/*',
    };
    (0, react_1.useEffect)(() => {
        if (value && value.id && value.url) {
            setFileList([{ uid: value.id, id: value.id, url: value.url }]);
        }
    }, [value]);
    const handlePreview = (file) => __awaiter(this, void 0, void 0, function* () {
        if (!file.url && !file.preview) {
            // eslint-disable-next-line no-param-reassign
            file.preview = yield getBase64(file.originFileObj);
        }
        setPreviewImage(file.url || file.preview);
        setPreviewVisible(true);
    });
    const handleChange = ({ fileList: newFileList }) => {
        if (newFileList.length > 0) {
            const validFile = newFileList[newFileList.length - 1];
            if (validFile.status === 'done') {
                const { response = {} } = validFile;
                const { id, url } = response.data || {};
                setFileList([
                    {
                        uid: validFile.uid,
                        // @ts-ignore
                        name: validFile.name,
                        status: validFile.status,
                        url,
                        id,
                    },
                ]);
                onChange({ url, id });
            }
            else {
                setFileList([validFile]);
            }
        }
        else {
            setFileList([]);
            onChange('');
        }
    };
    const handleCancel = () => {
        setPreviewVisible(false);
    };
    return (react_1.default.createElement(Container, null,
        react_1.default.createElement(antd_1.Upload, Object.assign({}, uploadProps, { 
            // @ts-ignore
            fileList: fileList, listType: "picture-card", onPreview: handlePreview, onChange: handleChange }),
            react_1.default.createElement("div", null,
                react_1.default.createElement(icons_1.PlusOutlined, null),
                react_1.default.createElement("div", { className: "ant-upload-text" }, "Upload"))),
        react_1.default.createElement(antd_1.Modal, { visible: previewVisible, width: "1080px", footer: null, onCancel: handleCancel },
            react_1.default.createElement("img", { alt: "example", style: { width: '100%' }, src: previewImage }))));
}
exports.default = CoverUpload;
