"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 __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 react_router_dom_1 = require("react-router-dom");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const index_1 = require("@/components/index");
const index_2 = require("@/constants/index");
const system_1 = require("@/pages/system");
const index_3 = require("@/utils/index");
const LocaleSelect = styled_components_1.default.div `
  border: 1px solid rgb(217, 217, 217);
  padding: 8px 0 0 8px;
`;
const Tips = styled_components_1.default.p `
  color: #f5222d;
  margin: 10px 0;
  font-size: 12px;
`;
const EditDrawer = (props) => {
    var _a, _b;
    const { saveLoading, drawerOpen, fileDetail, editContent, locales = [], baseContents, closeDrawer, fetchLocales, updateContentValue, updateContentTags, saveContent, } = props;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, content } = locale.business;
    // url search params
    const { applicationId, fileId } = (0, index_3.getLocationIfo)((0, react_router_dom_1.useLocation)());
    const localesTag = ((_a = editContent === null || editContent === void 0 ? void 0 : editContent.tags) === null || _a === void 0 ? void 0 : _a.filter((item) => item.locale)) || [];
    const queryTag = ((_b = editContent === null || editContent === void 0 ? void 0 : editContent.tags) === null || _b === void 0 ? void 0 : _b.find((item) => item.query)) || {};
    const isBase = editContent === null || editContent === void 0 ? void 0 : editContent.isBase;
    (0, react_1.useEffect)(() => {
        if (applicationId)
            fetchLocales(applicationId);
    }, []);
    const handleLocaleClick = (locale) => {
        let newLocalesTag = [];
        const selected = localesTag.find((item) => item.locale === locale);
        if (selected) {
            newLocalesTag = localesTag.filter((item) => item.locale !== locale);
        }
        else {
            Object.assign(newLocalesTag, localesTag);
            newLocalesTag.push({ locale });
        }
        if (queryTag.query) {
            updateContentValue('tags', [...newLocalesTag, queryTag]);
        }
        else {
            updateContentValue('tags', [...newLocalesTag]);
        }
    };
    return (react_1.default.createElement(index_1.OperationDrawer, { destroyOnClose: true, width: 480, title: (editContent === null || editContent === void 0 ? void 0 : editContent.id) ? content.edit : content.add, open: drawerOpen, onClose: () => {
            closeDrawer(false);
        }, actions: react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => {
                if (applicationId && fileId) {
                    saveContent({
                        applicationId,
                        fileId,
                        fileType: (fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.type) || index_2.FileType.page,
                    });
                }
            } },
            global.apply,
            saveLoading && react_1.default.createElement(icons_1.SyncOutlined, { spin: true, style: { color: '#fff' } })) }, editContent ? (react_1.default.createElement(index_1.Group, null,
        react_1.default.createElement(index_1.Field, null,
            react_1.default.createElement(index_1.Label, null, global.nameLabel),
            react_1.default.createElement(antd_1.Input, { value: editContent.title, placeholder: content.nameLabel, onChange: (e) => updateContentValue('title', e.target.value) })),
        !isBase && !(0, index_3.objectEmptyCheck)(locales) && (react_1.default.createElement(index_1.Field, null,
            react_1.default.createElement(index_1.Label, null, global.locale),
            react_1.default.createElement(LocaleSelect, null, locales.map((locale) => {
                const selected = localesTag.find((item) => item.locale === locale);
                return (react_1.default.createElement(antd_1.Tag, { key: locale, onClick: () => {
                        handleLocaleClick(locale);
                    }, color: selected ? 'green' : 'blue', style: { width: 70, textAlign: 'center', cursor: 'pointer', marginBottom: 8 } },
                    selected && react_1.default.createElement(icons_1.CheckOutlined, null),
                    locale));
            })))),
        !isBase && (react_1.default.createElement(index_1.Field, null,
            react_1.default.createElement(index_1.Label, null, content.query),
            react_1.default.createElement(index_1.JSONEditor, { jsonData: queryTag.query || {}, onChangeJSON: (json) => {
                    updateContentTags('query', json);
                }, onError: () => {
                    updateContentTags('query', undefined);
                } }))),
        !isBase && (react_1.default.createElement(index_1.Field, null,
            react_1.default.createElement(index_1.Label, null, content.extendTitle),
            react_1.default.createElement(Tips, null,
                react_1.default.createElement(icons_1.InfoCircleOutlined, null),
                " ",
                content.extendTips),
            react_1.default.createElement(antd_1.Select, { style: { width: '100%' }, value: editContent === null || editContent === void 0 ? void 0 : editContent.extendId, disabled: !!(editContent === null || editContent === void 0 ? void 0 : editContent.id), onChange: (value) => updateContentValue('extendId', value) }, baseContents.map((item) => (react_1.default.createElement(antd_1.Select.Option, { key: item.id, value: item.id }, item.name)))))))) : (react_1.default.createElement("div", null))));
};
exports.default = EditDrawer;
