"use strict";

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

var React = _interopRequireWildcard(require("react"));

var PIXI = _interopRequireWildcard(require("pixi.js"));

var _reactPixiFiber = require("react-pixi-fiber");

var _default2 = require("./constants/default");

var _index = require("./components/index");

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

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

PIXI.utils.skipHello();
/**
 * 渲染model
 *
 * @class Render
 * @implements {IRenderer}
 */

var Renderer =
/*#__PURE__*/
function () {
  function Renderer(template, selector, options) {
    _classCallCheck(this, Renderer);

    _defineProperty(this, "app", new PIXI.Application(800, 800, {
      antialias: true
    }));

    _defineProperty(this, "$el", void 0);

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

    this.$el = selector instanceof HTMLCanvasElement ? selector : document.querySelector(selector);

    if (!this.$el || !(this.$el instanceof HTMLCanvasElement)) {
      throw new Error('Need HTMLCanvasElement');
    }

    this.app = new PIXI.Application(options.width, options.height, _objectSpread({}, options, {
      view: this.$el
    }));
    this.template = template;
  }
  /**
   * 导出图片
   *
   * @returns
   * @memberof Render
   */


  _createClass(Renderer, [{
    key: "toDataURL",
    value: function toDataURL() {
      if (!this.$el) {
        console.warn('need excute Render.renderCanvas() before');
        return '';
      }

      return this.$el.toDataURL('image/png', 1);
    }
    /**
     * 渲染到 canvas
     *
     * @returns
     * @memberof Render
     */

  }, {
    key: "renderCanvas",
    value: function renderCanvas() {
      var _this = this;

      var download = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;

      var Component = function Component(props) {
        return props.type === 'poster' ? props.layouts.map(function (layout, index) {
          return React.createElement(_index.LayoutComponent, _extends({}, layout, {
            key: index,
            app: _this.app
          }), layout.elements ? layout.elements.map(function (element, index) {
            return _this.getComponent(element, _this, index);
          }) : null);
        }) : _this.getComponent(props, _this);
      };

      (0, _reactPixiFiber.render)(React.createElement(Component, this.template), this.app.stage);

      if (download) {
        setTimeout(function () {
          var a = document.createElement('a');
          a.download = _this.template.title || '下载图片';
          a.href = _this.toDataURL();
          a.click();
        }, 200);
      }

      return this.$el;
    }
  }, {
    key: "getComponent",
    value: function getComponent(element, context, index) {
      if (!element.type && !_default2.SUPPORT_TYPES.includes(element.type)) {
        throw new TypeError("element type error or doesn't exist");
      }

      var Component;

      switch (element.type) {
        case 'image':
          Component = React.createElement(_index.ImageComponent, _extends({}, element, {
            key: index,
            app: context.app
          }));
          break;

        case 'text':
          Component = React.createElement(_index.TextComponent, _extends({}, element, {
            key: index,
            app: context.app
          }));
          break;

        case 'svg':
          Component = React.createElement(_index.SvgComponent, _extends({}, element, {
            key: index,
            app: context.app
          }));
          break;

        case 'group':
          Component = React.createElement(_index.GroupComponent, _extends({}, element, {
            key: index,
            app: context.app
          }));
          break;

        case 'mask':
          Component = React.createElement(_index.MaskComponent, _extends({}, element, {
            key: index,
            app: context.app
          }));
          break;

        default:
          Component = React.createElement(_index.ImageComponent, _extends({}, element, {
            key: index,
            app: context.app
          }));
          break;
      }

      return Component;
    }
    /**
     * Custom pixiComponent of renderer
     * @see {@link https://github.com/michalochman/react-pixi-fiber}
     */

  }]);

  return Renderer;
}();

_defineProperty(Renderer, "CustomPIXIComponent", _reactPixiFiber.CustomPIXIComponent);

_defineProperty(Renderer, "PIXI", PIXI);

var _default = Renderer;
exports.default = _default;