import { tool } from "@opencode-ai/plugin";

export default tool({
  description:
    "在前端地图上对节点或管道图层应用样式，或重置为默认样式。样式参数应尽量与前端样式编辑器字段保持一致。",
  args: {
    reason: tool.schema
      .string()
      .describe(
        "Why this style action is needed for the current user request.",
      ),
    layer_id: tool.schema
      .enum(["junctions", "pipes"])
      .describe("Target layer id. Must be exactly 'junctions' or 'pipes'."),
    reset_to_default: tool.schema
      .boolean()
      .optional()
      .describe("Whether to reset the target layer to its default style."),
    style_config: tool.schema
      .object({
        property: tool.schema
          .string()
          .optional()
          .describe("Data property to render."),
        classification_method: tool.schema
          .enum(["pretty_breaks", "custom_breaks"])
          .optional()
          .describe("Classification method."),
        segments: tool.schema
          .number()
          .int()
          .min(2)
          .max(10)
          .optional()
          .describe("Number of rendered intervals, from 2 to 10."),
        min_size: tool.schema
          .number()
          .optional()
          .describe("Minimum point radius."),
        max_size: tool.schema
          .number()
          .optional()
          .describe("Maximum point radius."),
        min_stroke_width: tool.schema
          .number()
          .optional()
          .describe("Minimum line width."),
        max_stroke_width: tool.schema
          .number()
          .optional()
          .describe("Maximum line width."),
        fixed_stroke_width: tool.schema
          .number()
          .optional()
          .describe("Fixed line width when width is not data-driven."),
        color_type: tool.schema
          .enum(["single", "gradient", "rainbow", "custom"])
          .optional()
          .describe("Color strategy."),
        single_palette_index: tool.schema.number().int().min(0).max(6).optional(),
        gradient_palette_index: tool.schema.number().int().min(0).max(2).optional(),
        rainbow_palette_index: tool.schema.number().int().min(0).max(1).optional(),
        show_labels: tool.schema
          .boolean()
          .optional()
          .describe("Whether to show labels."),
        show_id: tool.schema
          .boolean()
          .optional()
          .describe("Whether to show ids."),
        opacity: tool.schema.number().min(0).max(1).optional().describe("Opacity in [0, 1]."),
        adjust_width_by_property: tool.schema
          .boolean()
          .optional()
          .describe("Whether line width is driven by the rendered property."),
        custom_breaks: tool.schema
          .array(tool.schema.number())
          .optional()
          .describe("Strictly increasing boundaries. Length must equal segments + 1."),
        custom_colors: tool.schema
          .array(tool.schema.string())
          .optional()
          .describe("Custom CSS colors. Length must equal segments."),
      })
      .optional()
      .describe(
        "Optional style config overrides. Omit when reset_to_default is true.",
      ),
  },
  async execute(args) {
    const layerLabel = args.layer_id === "junctions" ? "节点" : "管道";
    if (args.reset_to_default) {
      return `已提交${layerLabel}图层默认样式重置请求。`;
    }
    const style = args.style_config;
    if (style?.custom_breaks) {
      if (
        style.custom_breaks.some(
          (value, index, values) => index > 0 && value <= values[index - 1],
        )
      ) {
        throw new Error("custom_breaks must be strictly increasing");
      }
      if (style.segments && style.custom_breaks.length !== style.segments + 1) {
        throw new Error("custom_breaks length must equal segments + 1");
      }
    }
    if (style?.segments && style.custom_colors && style.custom_colors.length !== style.segments) {
      throw new Error("custom_colors length must equal segments");
    }
    return `已提交${layerLabel}图层样式请求。`;
  },
});
