"use strict";

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

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

var _filterAdjustment = require("@pixi/filter-adjustment");

var _filterKawaseBlur = require("@pixi/filter-kawase-blur");

var _event = _interopRequireDefault(require("./event"));

var _transform = _interopRequireDefault(require("../../utils/transform"));

var _utils = require("../../utils/utils");

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

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 _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 _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 _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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

/**
 * base adapter for basic propery convert
 *
 * @export
 * @class BaseAdapter
 * @extends {Event}
 * @implements {IBaseAdapter}
 */
var BaseAdapter =
/*#__PURE__*/
function (_Event) {
  _inherits(BaseAdapter, _Event);

  function BaseAdapter(props, option) {
    var _this;

    _classCallCheck(this, BaseAdapter);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(BaseAdapter).call(this, props, option ? !!option.autoSetEvent : true));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "x", 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "y", 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "visible", true);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "interactive", true);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "buttonMode", false);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "$editing", false);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "cursor", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "rotation", 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "anchor", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "pivot", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "skew", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "filters", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "scale", void 0);

    Object.assign(_assertThisInitialized(_assertThisInitialized(_this)), {
      x: props.left + props.width / 2 || 0,
      y: props.top + props.height / 2 || 0,
      visible: !!!props.hidden,
      interactive: !!!props.frozen,
      interactiveChildren: !!!props.frozen,
      cursor: props.cursor || 'pointer',
      alpha: props.opacity,
      // TODO: 改用 anchor
      // 设置中心点
      pivot: new PIXI.Point(props.width / 2, props.height / 2)
    });
    _this.$editing = props.$editing;

    if (props.transform) {
      var transform = (0, _transform.default)(props.transform);
      _this.skew = transform.skew;
      _this.rotation = transform.rotation; // fix shadow diff

      _this.scale = Object.assign({}, transform.scale);
    } // https://github.com/pixijs/pixi-filters


    if (props.filter) {
      var _props$filter = props.filter,
          _props$filter$hueRota = _props$filter.hueRotate,
          hueRotate = _props$filter$hueRota === void 0 ? 1 : _props$filter$hueRota,
          _props$filter$saturat = _props$filter.saturate,
          saturate = _props$filter$saturat === void 0 ? 1 : _props$filter$saturat,
          _props$filter$brightn = _props$filter.brightness,
          brightness = _props$filter$brightn === void 0 ? 1 : _props$filter$brightn,
          _props$filter$gaussia = _props$filter.gaussianBlur,
          gaussianBlur = _props$filter$gaussia === void 0 ? 1 : _props$filter$gaussia;
      _this.filters = [new _filterAdjustment.AdjustmentFilter({
        // 亮度
        brightness: Math.max(brightness / 10, 1),
        // 滤镜的伽玛值
        gamma: 1,
        // 饱和度
        saturation: Math.max(saturate / 10, 1),
        // 对比度
        contrast: 1,
        red: Math.max(hueRotate / 10, 1),
        green: Math.max(hueRotate / 10, 1),
        blue: Math.max(hueRotate / 10, 1),
        alpha: 1
      })];

      if (gaussianBlur) {
        _this.filters.push(new _filterKawaseBlur.KawaseBlurFilter(gaussianBlur / 10, 4, false));
      }
    } // delete some property


    (0, _utils.deleteProperty)(_assertThisInitialized(_assertThisInitialized(_this)), option && option.omit);
    return _possibleConstructorReturn(_this, _assertThisInitialized(_assertThisInitialized(_this)));
  }

  return BaseAdapter;
}(_event.default);

exports.default = BaseAdapter;