"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

exports.__esModule = true;
exports.useStyle = useStyle;
exports.setSkinLoader = setSkinLoader;
exports.loadSkin = loadSkin;

var config = _interopRequireWildcard(require("./config"));

var currentStyle;
var loader;
var styleSwitchDelay = 200;

function useStyle(style) {
  style.use();
  setTimeout(function () {
    if (currentStyle) {
      currentStyle.unuse();
    }

    currentStyle = style;
  }, styleSwitchDelay);
}

function setSkinLoader(l) {
  loader = l;
}

function loadSkin(brand) {
  //TODO Catch skin load error and load default
  return new Promise(function (resolve, reject) {
    if (!loader) {
      throw new Error('Skin loader have not been configured yet, check startup sequence');
    }

    if (undefined !== brand) {
      config.getConfig().brandId = brand;
    }

    resolve(loader((config.getConfig().brandId || '').toString()));
  }).then(function (skin) {
    useStyle(skin);
    return skin;
  });
}