"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setRenderer = exports.setEntityStyle = exports.getImage = exports.getColor = exports.UniqueValue = exports.Simple = exports.Renderer = exports.Polyline = exports.Polygon = exports.Point = exports.Marker = exports.ClassBreaks = void 0;
var _mapIcon = _interopRequireDefault(require("@vtx/map-icon/lib/map-icon"));
var _isObject = _interopRequireDefault(require("../_util/isObject"));
var _transformRgb = _interopRequireDefault(require("../_util/transformRgb"));
var _isFunction = _interopRequireDefault(require("../_util/isFunction"));
var _lodash = require("lodash");
var _excluded = ["type", "icon", "image", "color", "size", "text"],
  _excluded2 = ["type", "icon", "image", "color", "size", "mapIcon"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var _Cesium = Cesium,
  Color = _Cesium.Color,
  HorizontalOrigin = _Cesium.HorizontalOrigin,
  VerticalOrigin = _Cesium.VerticalOrigin,
  HeightReference = _Cesium.HeightReference;
;
var getImage = function getImage(options) {
  var image;
  if (typeof options === 'string') {
    image = options;
  } else if ((0, _isObject["default"])(options)) {
    var _options$type = options.type,
      type = _options$type === void 0 ? 'CommonIcon2' : _options$type,
      icon = options.icon,
      _image = options.image,
      color = options.color,
      size = options.size,
      text = options.text,
      other = _objectWithoutProperties(options, _excluded);
    color = color || '#0080ff';
    // 图标类型 CockpitIcon CockpitIcon2 CommonIcon CommonIcon2
    if (type === 'PointIcon') {
      image = new _mapIcon["default"][type](_objectSpread({
        size: size,
        color: color
      }, other)).getImgUrl();
    } else if (_mapIcon["default"][type] && text) {
      image = new _mapIcon["default"][type](_objectSpread({
        color: color,
        text: text
      }, other)).getImgUrl();
    } else if (_mapIcon["default"][type] && _mapIcon["default"].icon[icon]) {
      image = new _mapIcon["default"][type](_objectSpread({
        canvasIcon: icon,
        color: color,
        size: size
      }, other)).getImgUrl();
    } else {
      image = _image;
    }
  }
  return image;
};
exports.getImage = getImage;
var getColor = function getColor(style) {
  if ((0, _isObject["default"])(style)) {
    var color = style.color,
      opacity = style.opacity;
    return Color.fromCssColorString((0, _transformRgb["default"])(color, opacity));
  }
  if (typeof style === 'string') {
    return Color.fromCssColorString(style);
  }
};
exports.getColor = getColor;
var Point = function Point(options) {
  var radius = options.radius,
    fill = options.fill,
    stroke = options.stroke;
  var style = {
    pixelSize: radius // 以像素为单位指定大小
  };

  if (fill) {
    style = _objectSpread(_objectSpread({}, style), {}, {
      color: getColor(fill)
    });
  }
  if (stroke) {
    style = _objectSpread(_objectSpread({}, style), {}, {
      outline: true,
      outlineColor: getColor(stroke),
      outlineWidth: stroke.width || 2
    });
  }
  if (options !== null && options !== void 0 && options.ellipse) {
    style.ellipse = options.ellipse;
  }
  return style;
};
exports.Point = Point;
var Polyline = function Polyline(options) {
  var style = {
    width: 2
  };
  if (options !== null && options !== void 0 && options.stroke) {
    var stroke = options.stroke;
    style = _objectSpread(_objectSpread({}, style), {}, {
      width: stroke.width || 2,
      material: getColor(stroke)
    });
  }
  if (options !== null && options !== void 0 && options.material) {
    style.material = options.material;
  }
  return style;
};
exports.Polyline = Polyline;
var Polygon = function Polygon(options) {
  var style = _objectSpread({}, options);
  if (options) {
    var fill = options.fill,
      stroke = options.stroke,
      material = options.material;
    if (fill) {
      style = _objectSpread(_objectSpread({}, style), {}, {
        fill: true,
        material: material || getColor(fill)
      });
    }
    if (stroke) {
      style = _objectSpread(_objectSpread({}, style), {}, {
        outline: true,
        outlineColor: getColor(stroke),
        outlineWidth: stroke.width || 2
      });
    }
  }
  return style;
};
exports.Polygon = Polygon;
var Marker = function Marker(options) {
  var _ref = options || {},
    type = _ref.type,
    icon = _ref.icon,
    image = _ref.image,
    color = _ref.color,
    size = _ref.size,
    mapIcon = _ref.mapIcon,
    otherOptions = _objectWithoutProperties(_ref, _excluded2);
  var style = _objectSpread({
    horizontalOrigin: HorizontalOrigin.CENTER,
    verticalOrigin: VerticalOrigin.BOTTOM,
    heightReference: HeightReference.CLAMP_TO_GROUND,
    clampToGround: true,
    mapIcon: _objectSpread({}, options)
  }, otherOptions);
  if (options !== null && options !== void 0 && options.mapIcon) {
    style.image = getImage(options.mapIcon);
  } else if (options) {
    style.image = getImage(options);
  }
  if (options !== null && options !== void 0 && options.material) {
    style.material = options.material;
  }
  return style;
};
exports.Marker = Marker;
var TYPE = {
  Point: Point,
  Polyline: Polyline,
  Polygon: Polygon,
  Circle: Point,
  Marker: Marker
};
var Renderer = function Renderer(type) {
  var style = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  if (!type) {
    new Error('缺少类型');
    return;
  }
  if (!TYPE[type]) {
    new Error('没有这个类型类型的样式');
    return;
  }
  return TYPE[type](style);
};

// 简单渲染
exports.Renderer = Renderer;
var Simple = function Simple(_ref2) {
  var type = _ref2.type,
    style = _ref2.style;
  return Renderer(type, style);
};

// 唯一值渲染
exports.Simple = Simple;
var UniqueValue = function UniqueValue(feature, _ref3) {
  var field = _ref3.field,
    _ref3$render = _ref3.render,
    render = _ref3$render === void 0 ? [] : _ref3$render;
  var value = feature.properties.properties._value[field];
  var renderer = render.find(function (v) {
    return String(v.value) === String(value);
  });
  if (renderer) {
    return Simple(renderer);
  }
};

// 区间渲染
exports.UniqueValue = UniqueValue;
var ClassBreaks = function ClassBreaks(feature, _ref4) {
  var field = _ref4.field,
    _ref4$render = _ref4.render,
    render = _ref4$render === void 0 ? [] : _ref4$render;
  var value = feature.properties.properties._value[field];
  var renderer = render.find(function (v) {
    return Number(value) >= Number(v.value[0]) && Number(value) <= Number(v.value[1]);
  });
  if (renderer) {
    return Simple(renderer);
  }
};

// 设置图形对象样式
exports.ClassBreaks = ClassBreaks;
var setEntityStyle = function setEntityStyle(entity, style) {
  for (var i in style) {
    if (entity.hasOwnProperty(i)) {
      if (i === 'material') {
        entity[i] = style[i];
      } else if (entity[i].setValue) {
        entity[i].setValue(style[i]);
      } else {
        entity[i] = style[i];
      }
    } else {
      entity[i] = style[i];
    }
  }
};

// 设置渲染方式
exports.setEntityStyle = setEntityStyle;
var setRenderer = function setRenderer(entity, style) {
  var _style$style;
  if (style !== null && style !== void 0 && (_style$style = style.style) !== null && _style$style !== void 0 && _style$style.ellipse) {
    var _style$style2;
    entity.ellipse = style === null || style === void 0 || (_style$style2 = style.style) === null || _style$style2 === void 0 ? void 0 : _style$style2.ellipse;
  }
  if ((0, _isFunction["default"])(style)) {
    var _style = style(entity);
    entity.setStyle(Simple(_style));
  } else if ((0, _isObject["default"])(style)) {
    var mode = style.mode;
    if (mode) {
      if (mode === 'Simple') {
        entity.setStyle(Simple(style));
      }
      if (mode === 'UniqueValue') {
        entity.setStyle(UniqueValue(entity, style));
      }
      if (mode === 'ClassBreaks') {
        entity.setStyle(ClassBreaks(entity, style));
      }
    } else if (style.style) {
      entity.setStyle(style.style);
    } else {
      entity.setStyle(Simple(_objectSpread({}, style)));
    }
  } else if ((0, _lodash.isString)(style)) {
    // 如果为字符串，判断为内部图标
    var image = getImage({
      icon: style
    });
    entity.setStyle({
      image: image
    });
  }
};
exports.setRenderer = setRenderer;
//# sourceMappingURL=index.js.map
