"use strict";
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var styled_components_1 = __importStar(require("@xstyled/styled-components"));
var system_1 = require("@xstyled/system");
var react_draggable_1 = __importDefault(require("react-draggable"));
var Btn_1 = __importDefault(require("../shared-style/Btn"));
var Button_1 = __importDefault(require("../Button"));
var Icon_1 = __importDefault(require("../Icon/Icon"));
var ModalContext_1 = __importDefault(require("./ModalContext"));
var ModalWrapper = styled_components_1.default.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n  display: flex;\n  flex-direction: column;\n\n  position: fixed;\n\n  padding: 2 2 8;\n\n  top: 50px;\n\n  background-color: bg;\n\n  box-shadow: inset 1px 1px 0px 1px ", ",\n    inset 0 0 0 1px ", ", 1px 1px 0 1px ", ";\n\n  ", "\n  ", "\n"], ["\n  display: flex;\n  flex-direction: column;\n\n  position: fixed;\n\n  padding: 2 2 8;\n\n  top: 50px;\n\n  background-color: bg;\n\n  box-shadow: inset 1px 1px 0px 1px ", ",\n    inset 0 0 0 1px ", ", 1px 1px 0 1px ", ";\n\n  ",
    "\n  ",
    "\n"])), system_1.th('colors.white'), system_1.th('colors.grays.3'), system_1.th('colors.black'), function (_a) {
    var width = _a.width, height = _a.height;
    return "\n    width: " + (width ? width + "px" : 'auto') + ";\n    height: " + (height ? height + "px" : 'auto') + ";\n  ";
}, function (_a) {
    var active = _a.active;
    return active
        ? styled_components_1.css(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n          z-index: modal;\n        "], ["\n          z-index: modal;\n        "]))) : '';
});
var TitleBar = styled_components_1.default.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n  height: 18px;\n  margin-bottom: 2;\n\n  color: ", ";\n  padding: 2 2 0;\n\n  display: flex;\n  ", "\n"], ["\n  height: 18px;\n  margin-bottom: 2;\n\n  color: ", ";\n  padding: 2 2 0;\n\n  display: flex;\n  ", "\n"])), system_1.th('colors.white'), system_1.backgroundColor);
var Title = styled_components_1.default.div(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n  flex-grow: 1;\n  font-weight: bold;\n"], ["\n  flex-grow: 1;\n  font-weight: bold;\n"])));
var OptionsBox = styled_components_1.default.ul(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n  list-style: none;\n  padding: 0;\n  margin: 0;\n\n  display: flex;\n"], ["\n  list-style: none;\n  padding: 0;\n  margin: 0;\n\n  display: flex;\n"])));
var Option = styled_components_1.default(Btn_1.default)(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n  margin-right: 2;\n  padding: 0;\n\n  width: 17px;\n  height: 14px;\n  min-width: 0;\n\n  font-size: 10;\n\n  &:last-child {\n    margin-right: 0;\n  }\n\n  &:active {\n    padding: 1 0 0 1;\n\n    outline: none;\n  }\n\n  &:focus {\n    box-shadow: inset 1px 1px 0px 1px ", ",\n      inset -1px -1px 0px 1px ", ";\n  }\n"], ["\n  margin-right: 2;\n  padding: 0;\n\n  width: 17px;\n  height: 14px;\n  min-width: 0;\n\n  font-size: 10;\n\n  &:last-child {\n    margin-right: 0;\n  }\n\n  &:active {\n    padding: 1 0 0 1;\n\n    outline: none;\n  }\n\n  &:focus {\n    box-shadow: inset 1px 1px 0px 1px ", ",\n      inset -1px -1px 0px 1px ", ";\n  }\n"])), system_1.th('colors.white'), system_1.th('colors.grays.3'));
Option.displayName = 'Option';
var Content = styled_components_1.default.div(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n  flex-grow: 1;\n  display: flex;\n  flex-direction: column;\n\n  padding: 6;\n"], ["\n  flex-grow: 1;\n  display: flex;\n  flex-direction: column;\n\n  padding: 6;\n"])));
var ButtonWrapper = styled_components_1.default.div(templateObject_8 || (templateObject_8 = __makeTemplateObject(["\n  display: flex;\n  flex-direction: row;\n  justify-content: ", ";\n\n  padding: 0 6 6 6;\n\n  & ", " {\n    margin-right: 6;\n    min-width: 70px;\n\n    &:last-child {\n      margin-right: 0;\n    }\n  }\n"], ["\n  display: flex;\n  flex-direction: row;\n  justify-content: ", ";\n\n  padding: 0 6 6 6;\n\n  & ", " {\n    margin-right: 6;\n    min-width: 70px;\n\n    &:last-child {\n      margin-right: 0;\n    }\n  }\n"])), function (_a) {
    var _b = _a.buttonsAlignment, buttonsAlignment = _b === void 0 ? 'center' : _b;
    return buttonsAlignment;
}, Btn_1.default);
var MenuWrapper = styled_components_1.default.ul(templateObject_9 || (templateObject_9 = __makeTemplateObject(["\n  display: flex;\n  flex-direction: row;\n\n  list-style: none;\n  margin: 0;\n  padding-left: 0;\n  padding-bottom: 3;\n\n  border-bottom-style: solid;\n  border-width: 1;\n  border-bottom-color: grays.3;\n\n  box-shadow: 0 1px 0 0 ", ";\n"], ["\n  display: flex;\n  flex-direction: row;\n\n  list-style: none;\n  margin: 0;\n  padding-left: 0;\n  padding-bottom: 3;\n\n  border-bottom-style: solid;\n  border-width: 1;\n  border-bottom-color: grays.3;\n\n  box-shadow: 0 1px 0 0 ", ";\n"])), system_1.th('colors.grays.0'));
var MenuItem = styled_components_1.default.li(templateObject_11 || (templateObject_11 = __makeTemplateObject(["\n  position: relative;\n  padding-left: 6;\n  padding-right: 6;\n\n  user-select: none;\n\n  ul {\n    position: absolute;\n    left: 0;\n    color: ", ";\n  }\n\n  ", ";\n"], ["\n  position: relative;\n  padding-left: 6;\n  padding-right: 6;\n\n  user-select: none;\n\n  ul {\n    position: absolute;\n    left: 0;\n    color: ", ";\n  }\n\n  ",
    ";\n"])), system_1.th('colors.black'), function (_a) {
    var active = _a.active;
    return active && styled_components_1.css(templateObject_10 || (templateObject_10 = __makeTemplateObject(["\n      background-color: primary;\n      color: ", ";\n    "], ["\n      background-color: primary;\n      color: ", ";\n    "])), system_1.th('colors.white'));
});
MenuItem.displayName = 'MenuItem';
var ModalRenderer = function (_a, ref) {
    var buttons = _a.buttons, buttonsAlignment = _a.buttonsAlignment, children = _a.children, closeModal = _a.closeModal, defaultPosition = _a.defaultPosition, height = _a.height, icon = _a.icon, menu = _a.menu, title = _a.title, width = _a.width, rest = __rest(_a, ["buttons", "buttonsAlignment", "children", "closeModal", "defaultPosition", "height", "icon", "menu", "title", "width"]);
    var _b = React.useContext(ModalContext_1.default), addWindows = _b.addWindows, removeWindows = _b.removeWindows, setActiveWindow = _b.setActiveWindow, activeWindow = _b.activeWindow;
    var _c = React.useState(''), menuOpened = _c[0], setMenuOpened = _c[1];
    React.useEffect(function () {
        addWindows({ icon: icon, title: title });
        setActiveWindow(title);
        return function () { return removeWindows(title); };
    }, []);
    var iconStyle = {
        width: 15,
        height: 13,
        style: {
            marginRight: '4px',
        },
    };
    var isActive = title === activeWindow;
    return (React.createElement(react_draggable_1.default, { handle: ".draggable", defaultPosition: defaultPosition },
        React.createElement(ModalWrapper, __assign({ width: width, height: height }, rest, { onClick: function () { return setActiveWindow(title); }, active: isActive, ref: ref }),
            React.createElement(TitleBar, { className: "draggable", backgroundColor: isActive ? 'primary' : 'grays.3' },
                icon && React.createElement(Icon_1.default, __assign({ name: icon }, iconStyle)),
                React.createElement(Title, null, title),
                React.createElement(OptionsBox, null,
                    React.createElement(Option, null, "?"),
                    React.createElement(Option, { onClick: closeModal }, "x"))),
            menu && menu.length > 0 && (React.createElement(MenuWrapper, null, menu.map(function (_a) {
                var name = _a.name, list = _a.list;
                var active = menuOpened === name;
                return (React.createElement(MenuItem, { key: name, onMouseDown: function () { return setMenuOpened(name); }, active: active },
                    name,
                    active && list));
            }))),
            React.createElement(Content, { onClick: function () { return setMenuOpened(''); } }, children),
            buttons && buttons.length > 0 && (React.createElement(ButtonWrapper, { buttonsAlignment: buttonsAlignment }, buttons.map(function (button) { return (React.createElement(Button_1.default, { key: button.value, onClick: button.onClick, value: button.value }, button.value)); }))))));
};
var Modal = React.forwardRef(ModalRenderer);
Modal.displayName = 'Modal';
Modal.defaultProps = {
    icon: undefined,
    title: 'Modal',
    buttonsAlignment: 'flex-end',
    children: null,
    defaultPosition: { x: 0, y: 0 },
    buttons: [],
    menu: [],
    width: undefined,
    height: undefined,
    closeModal: function () { },
};
exports.default = Modal;
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11;
//# sourceMappingURL=Modal.js.map