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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import lines from "./lines";

// Get mapbox line-color expression.
const getLineColorExpr = lineProperty => {
  const expr = ["case"];
  Object.entries(lines).forEach(_ref => {
    let [key, {
      property,
      color
    }] = _ref;
    if (property === lineProperty) {
      expr.push(["==", ["get", property], key]);
      expr.push(color);
    }
  });
  expr.push("rgba(0, 0, 0, 0)");
  return expr;
};

// Generate dashed styles for features with multiple lines in the label.
const generateDashedStyleLayers = (line, layer) => {
  const {
    property,
    sourceId,
    sourceLayer
  } = layer;
  const styleLayers = [];
  const linesLabels = line.split(",");
  linesLabels.forEach((label, index) => {
    var _lines$label;
    const color = (_lines$label = lines[label]) === null || _lines$label === void 0 ? void 0 : _lines$label.color;
    if (!color) {
      // eslint-disable-next-line no-console
      console.log("There is no color defined for ".concat(label, ", available labels are "), lines);
      return;
    }
    styleLayers.push({
      id: "".concat(line).concat(index),
      source: sourceId,
      "source-layer": sourceLayer,
      type: "line",
      filter: ["==", line, ["get", property]],
      paint: {
        "line-color": color,
        "line-width": 4,
        "line-dasharray": [(linesLabels.length - index) * 2, index * 2]
      }
    });
  });
  return styleLayers;
};

// Load the tilestats data to get all the possible values for hauptlinie or touristische_linie property.
const getDashedStyleLayers = function (layer) {
  let zweitTilestats = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  const {
    property
  } = layer;
  const styleLayers = [];
  (zweitTilestats["geops.zweitausbildung.".concat(property)] || []).forEach(value => {
    const split = value.split(",");
    if (split.length > 1) {
      styleLayers.push(...generateDashedStyleLayers(value, layer));
    }
  });
  return styleLayers;
};
const getStyleLayers = function (layer) {
  let zweitTilestats = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  const {
    property,
    sourceId,
    sourceLayer,
    name,
    key
  } = layer;
  const defautStyle = {
    type: "line",
    filter: ["has", property],
    paint: {
      "line-color": getLineColorExpr(property),
      "line-width": 4
    },
    layout: {
      "line-cap": "round"
    }
  };
  const styleLayers = [// Lines with only one color.
  _objectSpread(_objectSpread({}, defautStyle), {}, {
    id: name || key,
    source: sourceId,
    "source-layer": sourceLayer
  }),
  // Lines with a dashed style.
  ...getDashedStyleLayers(layer, zweitTilestats)];

  // if a line has others sources to add, we add the corresponding style layer.
  Object.values(lines).forEach(_ref2 => {
    let {
      property: prop,
      extraSources,
      extraStyleLayers = []
    } = _ref2;
    if (prop !== property || !extraSources) {
      return;
    }
    Object.keys(extraSources).forEach((sourceKey, index) => {
      styleLayers.push(_objectSpread(_objectSpread({}, defautStyle), {}, {
        id: sourceKey,
        source: sourceKey
      }, extraStyleLayers[index] || {}));
    });
  });
  return styleLayers;
};
export default getStyleLayers;
//# sourceMappingURL=getStyleLayers.js.map