"use strict";

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

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

var _coreEvents = require("@storybook/core-events");

var _addons = _interopRequireDefault(require("@storybook/addons"));

var _prismjs = _interopRequireDefault(require("prismjs"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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; }

var Code =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Code, _React$Component);

  function Code(props, context) {
    var _this;

    _classCallCheck(this, Code);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Code).call(this, props, context));

    _defineProperty(_assertThisInitialized(_this), "onStoryChange", function () {
      _this.onSelectTab('');
    });

    _this.state = {
      code: ''
    };
    _this.channelName = "soft/code/add_".concat(props.type);
    _this.onSelectTab = _this.onSelectTab.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(Code, [{
    key: "onSelectTab",
    value: function onSelectTab(_ref) {
      var code = _ref.code,
          type = _ref.type;

      var formattedCode = type && code && _prismjs["default"].highlight(code, _prismjs["default"].languages[type]);

      this.setState({
        code: formattedCode
      });
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props = this.props,
          channel = _this$props.channel,
          api = _this$props.api;
      channel.on(this.channelName, this.onSelectTab);
      api.on(_coreEvents.STORY_CHANGED, this.onStoryChange);
    }
  }, {
    key: "render",
    value: function render() {
      var code = this.state.code;
      var _this$props2 = this.props,
          type = _this$props2.type,
          active = _this$props2.active;
      return active ? _react["default"].createElement("div", null, code ? _react["default"].createElement("pre", {
        className: "language-".concat(type)
      }, _react["default"].createElement("code", null, _react["default"].createElement("div", {
        dangerouslySetInnerHTML: {
          __html: code
        }
      }))) : _react["default"].createElement("p", null, " No ", type, " code Found ")) : null;
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      api.off(_coreEvents.STORY_CHANGED, this.onStoryChange);
      this.unmounted = true;
      var _this$props3 = this.props,
          channel = _this$props3.channel,
          api = _this$props3.api;
      channel.removeListener(this.channelName, this.onSelectTab);
    }
  }]);

  return Code;
}(_react["default"].Component);

var registerTab = function registerTab(_ref2) {
  var label = _ref2.label,
      type = _ref2.type;
  Promise.resolve().then(function () {
    return _interopRequireWildcard(require("prismjs/components/prism-".concat(type, ".js")));
  });

  _addons["default"].register("soft/code/add_".concat(type), function (api) {
    _addons["default"].addPanel("soft/".concat(type, "/panel"), {
      title: label,
      render: function render(_ref3) {
        var active = _ref3.active,
            key = _ref3.key;
        return _react["default"].createElement(Code, {
          active: active,
          api: api,
          channel: _addons["default"].getChannel(),
          key: key,
          type: type
        });
      }
    });
  });
};

var configure = function configure(_ref4) {
  var _ref4$theme = _ref4.theme,
      theme = _ref4$theme === void 0 ? 'prism' : _ref4$theme,
      _ref4$tabs = _ref4.tabs,
      tabs = _ref4$tabs === void 0 ? [] : _ref4$tabs;
  Promise.resolve().then(function () {
    return _interopRequireWildcard(require("prismjs/themes/".concat(theme, ".css")));
  });
  var tabsToRender = [].concat(tabs);
  tabsToRender.forEach(function (t) {
    return registerTab(t);
  });
};

exports.configure = configure;