"use strict";
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)
            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 });
const react_1 = __importDefault(require("react"));
const theme_1 = require("components/config/theme");
const classnames_1 = __importDefault(require("classnames"));
const lodash_1 = __importDefault(require("lodash"));
const react_jss_1 = __importDefault(require("react-jss"));
const withStyles = (styles) => (component) => {
    const StyledComponent = react_jss_1.default(styles, { theming: theme_1.theming })(component);
    class Wrapper extends react_1.default.Component {
        constructor() {
            super(...arguments);
            this.state = {
                classes: {},
            };
            this._mergeClasses = (props) => {
                // Merge classes generated by JSS and user classes
                let oldClasses = this.styledComponent.state.classes;
                let userClasses = props.classes || {};
                Object.keys(oldClasses).forEach((classKey) => {
                    oldClasses[classKey] = classnames_1.default(oldClasses[classKey], userClasses[classKey]);
                });
                this.setState({ classes: oldClasses });
            };
        }
        componentDidMount() {
            this._mergeClasses(this.props);
        }
        componentDidUpdate(prevProps) {
            if (!lodash_1.default.isEqual(this.props.classes, prevProps.classes)) {
                this._mergeClasses(this.props);
            }
        }
        render() {
            let _a = this.props, { classes } = _a, rest = __rest(_a, ["classes"]); // eslint-disable-line
            return (react_1.default.createElement(StyledComponent, Object.assign({ ref: (ref) => {
                    this.styledComponent = ref;
                } }, rest, { classes: this.state.classes })));
        }
    }
    Wrapper.displayName = StyledComponent.displayName;
    return Wrapper;
};
exports.composeStyles = (...styles) => (theme) => {
    let mergedStyles = {};
    styles.forEach((v, i) => {
        const style = v(theme);
        mergedStyles = Object.assign({}, mergedStyles, style);
    });
    return mergedStyles;
};
exports.default = withStyles;
//# sourceMappingURL=index.js.map