"use strict";

require("core-js/modules/web.dom-collections.iterator.js");

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

var _react = require("react");

var _reactSingletonHook = require("react-singleton-hook");

var _colors = require("subfirelib/utils/colors");

var _materialColor = require("subfirelib/utils/material-color");

var _styles = require("@material-ui/core/styles");

var _amber = _interopRequireDefault(require("@material-ui/core/colors/amber"));

var _blue = _interopRequireDefault(require("@material-ui/core/colors/blue"));

var _blueGrey = _interopRequireDefault(require("@material-ui/core/colors/blueGrey"));

var _brown = _interopRequireDefault(require("@material-ui/core/colors/brown"));

var _cyan = _interopRequireDefault(require("@material-ui/core/colors/cyan"));

var _deepOrange = _interopRequireDefault(require("@material-ui/core/colors/deepOrange"));

var _deepPurple = _interopRequireDefault(require("@material-ui/core/colors/deepPurple"));

var _green = _interopRequireDefault(require("@material-ui/core/colors/green"));

var _grey = _interopRequireDefault(require("@material-ui/core/colors/grey"));

var _indigo = _interopRequireDefault(require("@material-ui/core/colors/indigo"));

var _lightBlue = _interopRequireDefault(require("@material-ui/core/colors/lightBlue"));

var _lightGreen = _interopRequireDefault(require("@material-ui/core/colors/lightGreen"));

var _lime = _interopRequireDefault(require("@material-ui/core/colors/lime"));

var _orange = _interopRequireDefault(require("@material-ui/core/colors/orange"));

var _pink = _interopRequireDefault(require("@material-ui/core/colors/pink"));

var _purple = _interopRequireDefault(require("@material-ui/core/colors/purple"));

var _red = _interopRequireDefault(require("@material-ui/core/colors/red"));

var _teal = _interopRequireDefault(require("@material-ui/core/colors/teal"));

var _yellow = _interopRequireDefault(require("@material-ui/core/colors/yellow"));

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

const colors = {
  amber: _amber.default,
  blue: _blue.default,
  blueGrey: _blueGrey.default,
  brown: _brown.default,
  cyan: _cyan.default,
  deepOrange: _deepOrange.default,
  deepPurple: _deepPurple.default,
  green: _green.default,
  grey: _grey.default,
  indigo: _indigo.default,
  lightBlue: _lightBlue.default,
  lightGreen: _lightGreen.default,
  lime: _lime.default,
  orange: _orange.default,
  pink: _pink.default,
  purple: _purple.default,
  red: _red.default,
  teal: _teal.default,
  yellow: _yellow.default
};
const indexes = ['50', '100', '200', '300', '400', '500', '600', '700', '800', '900']; // const aIndex = ['A100', 'A200', 'A400', 'A700'];

function findIndexes(key) {
  let m, l, d;

  if (key.startsWith('A')) {
    if (key === 'A100') key = '200';
    if (key === 'A200') key = '400';
    if (key === 'A400') key = '500';
    if (key === 'A700') key = '700';
  }

  m = key;
  const i = indexes.indexOf(m);
  l = i - 2;
  if (l < 0) l = 0;
  l = indexes[l];
  d = i + 2;
  if (d < 9) d = 9;
  d = indexes[d];
  return {
    m,
    l,
    d
  };
}

function parseColor(cname) {
  let c = cname.split('-');

  if (c.length === 2) {// console.log(c);
  } else {
    console.warn('oops', c);
  }

  return c;
}

function genThemePaletteFromColor(c, p) {
  const mc = (0, _materialColor.colorToMaterial)(...c);
  let colorName = mc.closestMaterialName; // console.log(colorName);
  // brown is boring...
  // if (colorName.startsWith('brown')) {
  //   colorName = colorToMaterial(...p[3]).closestMaterialName;
  //   // console.log('replaced with ', colorName)
  // }
  // console.log(mc);

  let colorKeys = parseColor(colorName);

  if (colorKeys.length === 2) {
    if (colorKeys[0] === 'black') colorKeys = ['grey', '800'];
    if (colorKeys[0] === 'white') colorKeys = ['grey', '400'];
    const ac = colors[colorKeys[0]]; // console.log(ac);

    const idx = findIndexes(colorKeys[1]); // if (!ac || !idx) {
    //   debugger;
    // }

    return {
      light: ac[idx.l],
      main: ac[idx.m],
      dark: ac[idx.d]
    };
  } else {
    return null;
  }
}

function useImageColorThemeImpl() {
  const appTheme = (0, _styles.useTheme)();
  const [imageTag, setImageTag] = (0, _react.useState)({
    tag: null
  });
  const [palette, setPalette] = (0, _react.useState)(null);
  const [theme, setTheme] = (0, _react.useState)(appTheme);
  (0, _react.useEffect)(() => {
    if (!(imageTag !== null && imageTag !== void 0 && imageTag.tag)) return;
    const p = (0, _colors.getPalette)(imageTag.tag);
    setPalette(p);
  }, [setPalette, imageTag]);
  (0, _react.useEffect)(() => {
    if (!(palette !== null && palette !== void 0 && palette.length)) {
      setTheme(appTheme);
      return;
    }

    const primary = genThemePaletteFromColor(palette[0], palette);
    const secondary = genThemePaletteFromColor(palette[1], palette);
    const config = {
      palette: {
        primary: primary,
        secondary: secondary,
        type: 'dark'
      }
    };
    const theme = (0, _styles.createMuiTheme)(config);
    setTheme(theme);
  }, [palette, setTheme, appTheme]);
  return {
    imageTag,
    setImageTag,
    palette,
    theme
  };
}

const useImageColorTheme = (0, _reactSingletonHook.singletonHook)({}, useImageColorThemeImpl);
exports.useImageColorTheme = useImageColorTheme;
var _default = useImageColorTheme;
exports.default = _default;