"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
var jsx_runtime_1 = require("react/jsx-runtime");
var antd_1 = require("antd");
var react_1 = require("react");
var lodash_1 = require("lodash");
var misc_1 = require("@cocalc/util/misc");
var Option = antd_1.Select.Option;
function SelectWithDefault(_a) {
    var e_1, _b;
    var _c, _d, _e;
    var value = _a.value, defaultValue = _a.defaultValue, initialValue = _a.initialValue, onChange = _a.onChange, options = _a.options, style = _a.style;
    var _f = __read((0, react_1.useState)((_e = (_d = (_c = value !== null && value !== void 0 ? value : initialValue) !== null && _c !== void 0 ? _c : defaultValue) !== null && _d !== void 0 ? _d : (0, lodash_1.keys)(options)[0]) !== null && _e !== void 0 ? _e : ""), 2), val = _f[0], setVal = _f[1];
    var v = [];
    if ((0, misc_1.is_array)(options)) {
        try {
            // @ts-ignore
            for (var options_1 = __values(options), options_1_1 = options_1.next(); !options_1_1.done; options_1_1 = options_1.next()) {
                var value_1 = options_1_1.value;
                v.push((0, jsx_runtime_1.jsx)(Option, __assign({ value: value_1 }, { children: value_1 }), value_1));
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (options_1_1 && !options_1_1.done && (_b = options_1.return)) _b.call(options_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    else {
        for (var value_2 in options) {
            v.push((0, jsx_runtime_1.jsx)(Option, __assign({ value: value_2 }, { children: options[value_2] }), value_2));
        }
    }
    return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: { width: "100%" } }, { children: [(0, jsx_runtime_1.jsx)(antd_1.Select, __assign({ showSearch: true, value: val, onChange: function (value) {
                    onChange(value);
                    setVal(value);
                }, style: __assign({ width: "30ex", maxWidth: "100%" }, style) }, { children: v })), defaultValue != null && ((0, jsx_runtime_1.jsx)(antd_1.Button, __assign({ type: "dashed", disabled: (value !== null && value !== void 0 ? value : val) == defaultValue, style: { marginLeft: "5px" }, onClick: function () {
                    onChange(defaultValue);
                    setVal(defaultValue);
                } }, { children: (value !== null && value !== void 0 ? value : val) == defaultValue ? ("Default") : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Changed from ", (0, misc_1.is_array)(options) ? defaultValue : options[defaultValue]] })) })))] })));
}
exports.default = SelectWithDefault;
//# sourceMappingURL=select-with-default.js.map