import type { ExpressionSpecification } from "maplibre-gl";

import { MAP_STYLE_TOKENS } from "./map-colors";

export const SUPPLY_INACTIVE_VALUES = [
  "closed",
  "off",
  "inactive",
  "关闭"
] as const;

export const supplyInactiveExpression: ExpressionSpecification = [
  "in",
  [
    "downcase",
    [
      "to-string",
      ["coalesce", ["get", "status"], ["get", "setting"], ""]
    ]
  ],
  ["literal", SUPPLY_INACTIVE_VALUES]
];

export const supplyPipeColorExpression: ExpressionSpecification = [
  "case",
  supplyInactiveExpression,
  MAP_STYLE_TOKENS.state.inactive,
  [">=", ["to-number", ["coalesce", ["get", "diameter"], 0]], 600],
  MAP_STYLE_TOKENS.supply.pipeMajor,
  [">=", ["to-number", ["coalesce", ["get", "diameter"], 0]], 300],
  MAP_STYLE_TOKENS.supply.pipeMedium,
  MAP_STYLE_TOKENS.supply.pipeMinor
];

export const supplyPipeDashExpression = [1, 0] as [number, number];

export function createSupplyActiveOpacity(
  activeOpacity: number
): ExpressionSpecification {
  return ["case", supplyInactiveExpression, 0, activeOpacity];
}

export const supplyFacilityOpacityExpression: ExpressionSpecification = [
  "case",
  supplyInactiveExpression,
  0.55,
  0.94
];

export const supplyIconOpacityExpression: ExpressionSpecification = [
  "case",
  supplyInactiveExpression,
  0.62,
  0.98
];

export function createSupplyStateColor(
  activeColor: string | ExpressionSpecification
): ExpressionSpecification {
  return [
    "case",
    supplyInactiveExpression,
    MAP_STYLE_TOKENS.state.inactive,
    activeColor
  ];
}
