"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ThemeConsumer = exports.default = exports.ThemeContext = void 0;

var _react = _interopRequireDefault(require("react"));

var _deepmerge = _interopRequireDefault(require("deepmerge"));

var _darkTheme = _interopRequireDefault(require("./darkTheme"));

var _ = require("./");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const ThemeContext = /*#__PURE__*/_react.default.createContext({
  theme: _.theme
});

exports.ThemeContext = ThemeContext;

class ThemeProvider extends _react.default.Component {
  constructor(props) {
    super(props);

    _defineProperty(this, "defaultTheme", void 0);

    _defineProperty(this, "state", void 0);

    _defineProperty(this, "updateTheme", updates => {
      this.setState(({
        newTheme
      }) => ({
        theme: (0, _deepmerge.default)(newTheme, updates)
      }));
    });

    _defineProperty(this, "replaceTheme", newTheme => {
      this.setState(() => ({
        theme: (0, _deepmerge.default)(this.defaultTheme, newTheme)
      }));
    });

    _defineProperty(this, "getTheme", () => this.state.theme);

    const defaultColors = props.useDark ? _darkTheme.default : _.theme;
    this.defaultTheme = (0, _deepmerge.default)({
      colors: defaultColors
    }, props.theme);
    this.state = {
      theme: this.defaultTheme,
      useDark: props.useDark
    };
  }

  static getDerivedStateFromProps(props, state) {
    const {
      useDark
    } = props;

    if (useDark !== state.useDark) {
      const defaultColors = useDark ? _darkTheme.default : _.theme;
      return {
        theme: (0, _deepmerge.default)(state.theme, {
          colors: defaultColors
        }),
        useDark
      };
    }

    return null;
  }

  render() {
    return /*#__PURE__*/_react.default.createElement(ThemeContext.Provider, {
      value: {
        theme: this.state.theme,
        //@ts-ignore
        updateTheme: this.updateTheme,
        replaceTheme: this.replaceTheme
      }
    }, this.props.children);
  }

}

exports.default = ThemeProvider;
const ThemeConsumer = ThemeContext.Consumer;
exports.ThemeConsumer = ThemeConsumer;
//# sourceMappingURL=ThemeProvider.js.map