"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 icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const system_1 = require("@/pages/system");
const { Option } = antd_1.Select;
const FlexRow = styled_components_1.default.div `
  display: flex;
  > div {
    flex: 1;
  }
`;
const Col = styled_components_1.default.div `
  display: flex;
  > div {
    flex-grow: 1;
  }
`;
function Category(props) {
    const [levelOneStatus, setLevelOneStatus] = (0, react_1.useState)(true);
    const [levelTwoStatus, setLevelTwoStatus] = (0, react_1.useState)(true);
    const [groups, setGroups] = (0, react_1.useState)([]);
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { category: i18n } = locale.business;
    const { categoryName, groupName, categories = [], onChange } = props;
    (0, react_1.useEffect)(() => {
        const _category = categories.find((item) => item.categoryName === categoryName);
        if (categoryName && _category) {
            setGroups(_category.groupNames);
        }
        else {
            setGroups([]);
        }
    }, [categoryName, categories]);
    const handleChange = (key, value) => {
        if (value.indexOf('.') > -1) {
            return;
        }
        onChange(key, value);
    };
    return (react_1.default.createElement("div", null,
        react_1.default.createElement(FlexRow, { style: { fontSize: '12px', color: '#828181' } },
            react_1.default.createElement("div", { style: { marginRight: 16 } },
                i18n.levelOne,
                " ",
                react_1.default.createElement("span", { style: { color: 'red' } }, "*")),
            react_1.default.createElement("div", { style: { marginLeft: 16 } },
                i18n.levelTwo,
                " ",
                react_1.default.createElement("span", { style: { color: 'red' } }, "*"))),
        react_1.default.createElement(FlexRow, null,
            react_1.default.createElement(Col, { style: { marginRight: 16 } },
                levelOneStatus ? (react_1.default.createElement(antd_1.Select, { style: { width: '100%' }, value: categoryName, placeholder: "Select category", onChange: (value) => handleChange('categoryName', value) }, categories.map((item) => (react_1.default.createElement(Option, { key: item.categoryName }, item.categoryName))))) : (react_1.default.createElement(antd_1.Input, { value: categoryName, style: { width: '100%' }, placeholder: "Input category", onChange: (e) => handleChange('categoryName', e.target.value) })),
                react_1.default.createElement(antd_1.Tooltip, { title: `Change to ${levelOneStatus ? 'customize' : 'select'} mode` },
                    react_1.default.createElement(antd_1.Button, { style: { marginLeft: 2, fontSize: '12px' }, onClick: () => setLevelOneStatus(!levelOneStatus) }, levelOneStatus ? react_1.default.createElement(icons_1.EditOutlined, null) : react_1.default.createElement(icons_1.BarsOutlined, null)))),
            react_1.default.createElement(Col, { style: { marginLeft: 16 } },
                levelTwoStatus ? (react_1.default.createElement(antd_1.Select, { style: { width: '100%' }, value: groupName, placeholder: "Select category", onChange: (value) => handleChange('groupName', value) }, groups.map((item) => (react_1.default.createElement(Option, { key: item }, item))))) : (react_1.default.createElement(antd_1.Input, { value: groupName, style: { width: '100%' }, placeholder: "Input category", onChange: (e) => handleChange('groupName', e.target.value) })),
                react_1.default.createElement(antd_1.Tooltip, { placement: "topLeft", title: `Change to ${levelTwoStatus ? 'customize' : 'select'} mode` },
                    react_1.default.createElement(antd_1.Button, { style: { marginLeft: 2, fontSize: '12px' }, onClick: () => setLevelTwoStatus(!levelTwoStatus) }, levelTwoStatus ? react_1.default.createElement(icons_1.EditOutlined, null) : react_1.default.createElement(icons_1.BarsOutlined, null)))))));
}
exports.default = Category;
