"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 });
exports.ExpandableSearchInput = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var classnames_1 = __importDefault(require("classnames"));
var ramda_1 = require("ramda");
var Close_1 = __importDefault(require("@mui/icons-material/Close"));
var IconButton_1 = __importDefault(require("@mui/material/IconButton"));
var Input_1 = __importDefault(require("@mui/material/Input"));
var InputAdornment_1 = __importDefault(require("@mui/material/InputAdornment"));
var Search_1 = __importDefault(require("@mui/icons-material/Search"));
var SmallIconButton_1 = require("../SmallIconButton");
var events_1 = require("../helpers/events");
var ExpandableSearchInput_module_css_1 = __importDefault(require("./ExpandableSearchInput.module.css"));
var ExpandableSearchInput = function (_a) {
    var query = _a.query, onChange = _a.onChange, classes = _a.classes;
    var _b = (0, react_1.useState)(query), value = _b[0], setValue = _b[1];
    var _c = (0, react_1.useState)(!!query), isOpen = _c[0], setIsOpen = _c[1];
    (0, react_1.useEffect)(function () {
        setValue(query);
    }, [query]);
    return isOpen ? (react_1.default.createElement(Input_1.default, { "data-reltio-id": "expandable-search-input", className: ExpandableSearchInput_module_css_1.default.input, value: value, autoFocus: true, placeholder: ui_i18n_1.default.text('Search'), disableUnderline: true, onChange: (0, ramda_1.pipe)(events_1.getValue, setValue), startAdornment: react_1.default.createElement(InputAdornment_1.default, { position: "start" },
            react_1.default.createElement(Search_1.default, { className: ExpandableSearchInput_module_css_1.default.startAdornment })), endAdornment: react_1.default.createElement(InputAdornment_1.default, { position: "end" },
            react_1.default.createElement(SmallIconButton_1.SmallIconButton, { size: "S", icon: Close_1.default, className: ExpandableSearchInput_module_css_1.default.endAdornment, onClick: (0, ramda_1.pipe)(ramda_1.F, setIsOpen, (0, ramda_1.always)(''), (0, ramda_1.tap)(onChange), setValue) })), onKeyPress: (0, ramda_1.when)((0, ramda_1.propEq)('key', 'Enter'), (0, ramda_1.pipe)((0, ramda_1.always)(value), ramda_1.trim, onChange)) })) : (react_1.default.createElement(IconButton_1.default, { "data-reltio-id": "expandable-search-button", className: (0, classnames_1.default)(ExpandableSearchInput_module_css_1.default.searchButton, classes === null || classes === void 0 ? void 0 : classes.searchButton), onClick: (0, ramda_1.pipe)(ramda_1.T, setIsOpen), size: "large" },
        react_1.default.createElement(Search_1.default, null)));
};
exports.ExpandableSearchInput = ExpandableSearchInput;
