import { Schema } from "effect";
import { Expression } from "../../../../../share/expression.js";
import { Locale } from "../../../../../share/locale.js";
import { DataSource } from "../../../../table/schema.js";
export { getStructFieldDescription, getStructFieldTitle } from "../../../../table/utils/schema-meta.js";
export const type = "com.shwfed.block.chart.xy";
export const compatibilityDate = "2026-05-17";
export const metadata = {
  name: "\u7B1B\u5361\u5C14\u5750\u6807\u56FE\u8868",
  icon: "fluent:chart-multiple-20-regular",
  w: { initial: 20, min: 8, max: Infinity },
  h: { initial: 9, min: 5, max: Infinity }
};
export const CurveType = Schema.Literal(
  "linear",
  "monotoneX",
  "natural",
  "basis",
  "cardinal",
  "catmullRom",
  "step",
  "stepAfter",
  "stepBefore"
);
export const ChartType = Schema.Literal(
  "line",
  "area",
  "groupedBar",
  "scatter",
  "stackedBar"
);
const isListType = (t) => t === "dyn" || t.startsWith("list") || t.startsWith("optional");
const isNumberType = (t) => t === "dyn" || t === "number" || t.startsWith("optional");
function listExpression(configure) {
  return Expression({
    configure,
    resultType: isListType,
    unlistedVariablesAreDyn: true
  });
}
function numberExpression(configure) {
  return Expression({
    configure,
    resultType: isNumberType,
    unlistedVariablesAreDyn: true
  });
}
export function dataSourceSchema(configure) {
  const base = DataSource(configure);
  return Schema.Struct({
    id: Schema.UUID.annotations({ description: "\u6570\u636E\u6E90\u552F\u4E00\u6807\u8BC6" }),
    name: Schema.String.pipe(Schema.pattern(/^[a-z_]\w*$/i)).annotations({
      title: "\u53D8\u91CF\u540D",
      description: "\u5728 X / Y \u8868\u8FBE\u5F0F\u4E2D\u5F15\u7528\u8BE5\u6570\u636E\u6E90\u7684 CEL \u6807\u8BC6\u7B26\uFF08\u5B57\u6BCD\u6216\u4E0B\u5212\u7EBF\u5F00\u5934\uFF0C\u4EC5\u542B\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\uFF09"
    }),
    // Chart-specific phrasing wins for chart UX: the `data` description ends
    // with "结果以上面的变量名暴露给 X / Y", which is chart-only context.
    request: base.fields.request.annotations({
      title: "\u8BF7\u6C42",
      description: "\u53EF\u9009\u7684 HTTP \u8BF7\u6C42\u8868\u8FBE\u5F0F\uFF1B\u672A\u914D\u7F6E\u65F6\u6570\u636E\u76F4\u63A5\u4ECE `\u6570\u636E` \u8868\u8FBE\u5F0F\u8BFB\u53D6\uFF08\u901A\u5E38\u5F15\u7528\u4E0A\u4E0B\u6587\u53D8\u91CF\uFF09"
    }),
    data: base.fields.data.annotations({
      title: "\u6570\u636E",
      description: "\u8FD4\u56DE\u4E00\u4E2A\u5217\u8868\uFF08`list<dyn>`\uFF09\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u914D\u7F6E\u4E86 `\u8BF7\u6C42` \u65F6\u53EF\u901A\u8FC7 `json` \u5F15\u7528\u54CD\u5E94\u4F53\u3002\u7ED3\u679C\u4EE5\u4E0A\u9762\u7684\u53D8\u91CF\u540D\u66B4\u9732\u7ED9 X / Y"
    })
  }).annotations({
    title: "\u6570\u636E\u6E90",
    description: "\u4E00\u4E2A\u547D\u540D\u6570\u636E\u6E90\uFF1B\u5176\u7ED3\u679C\u5217\u8868\u4EE5\u53D8\u91CF\u540D\u63D0\u4F9B\u7ED9 X / Y \u8868\u8FBE\u5F0F"
  });
}
export function ySeriesSchema(configure) {
  return Schema.Struct({
    id: Schema.UUID.annotations({ description: "Y \u7CFB\u5217\u552F\u4E00\u6807\u8BC6" }),
    value: listExpression(configure).annotations({
      title: "Y \u8868\u8FBE\u5F0F",
      description: "\u8FD4\u56DE\u4E00\u4E2A\u5217\u8868\u4F5C\u4E3A\u8BE5\u7CFB\u5217\u7684 Y \u503C\uFF0C\u4E0E X \u6309\u4E0B\u6807\u5BF9\u9F50\uFF1B\u53EF\u5F15\u7528\u6570\u636E\u6E90\u53D8\u91CF\uFF0C\u5982 `source1.map(r, r.value)`"
    }),
    label: Schema.optional(Locale).annotations({
      title: "\u6807\u7B7E",
      description: "\u56FE\u4F8B\u4E0E\u63D0\u793A\u6846\u4E2D\u663E\u793A\u7684\u7CFB\u5217\u540D\uFF1B\u7559\u7A7A\u5219\u7528\u8868\u8FBE\u5F0F\u539F\u6587"
    }),
    chart: Schema.optionalWith(ChartType, { default: () => "line" }).annotations({
      title: "\u56FE\u8868\u7C7B\u578B",
      description: "\u8BE5\u7CFB\u5217\u7684\u6E32\u67D3\u65B9\u5F0F\uFF1A\u6298\u7EBF / \u9762\u79EF / \u5206\u7EC4\u67F1 / \u6563\u70B9 / \u5806\u53E0\u67F1"
    }),
    color: Schema.optional(Schema.String).annotations({
      title: "\u989C\u8272",
      description: "\u7CFB\u5217\u4E3B\u8272\uFF08CSS \u989C\u8272\u503C\uFF09\u2014\u2014\u6298\u7EBF\u63CF\u8FB9 / \u9762\u79EF\u586B\u5145 / \u67F1\u4F53 / \u6563\u70B9\uFF1B\u7559\u7A7A\u5219\u6309\u8C03\u8272\u677F\u81EA\u52A8\u53D6\u8272"
    }),
    // line
    curveType: Schema.optionalWith(CurveType, { default: () => "monotoneX" }).annotations({
      title: "\u66F2\u7EBF\u7C7B\u578B",
      description: "\u6298\u7EBF\u5728\u76F8\u90BB\u6570\u636E\u70B9\u4E4B\u95F4\u7684\u63D2\u503C\u65B9\u5F0F\uFF08\u5BF9\u5E94 unovis `CurveType`\uFF09\uFF1B\u4EC5\u6298\u7EBF\u7C7B\u578B\u751F\u6548"
    }),
    // line / area
    lineWidth: Schema.optionalWith(
      Schema.Number.pipe(Schema.positive()),
      { default: () => 2 }
    ).annotations({
      title: "\u7EBF\u5BBD",
      description: "\u7EBF\u6761\u5BBD\u5EA6\uFF08\u50CF\u7D20\uFF09\uFF1B\u6298\u7EBF\u7C7B\u578B\u4E3A\u6298\u7EBF\u672C\u8EAB\uFF0C\u9762\u79EF\u7C7B\u578B\u4E3A\u9876\u90E8\u63CF\u8FB9\u7EBF"
    }),
    // line
    dash: Schema.optional(listExpression(configure)).annotations({
      title: "\u865A\u7EBF",
      description: "\u8FD4\u56DE\u4E00\u4E2A\u6570\u5B57\u5217\u8868\u4F5C\u4E3A\u300C\u7EBF\u6BB5 / \u7A7A\u767D\u300D\u865A\u7EBF\u6A21\u5F0F\uFF08\u5982 `[4, 4]`\uFF09\uFF1B\u7559\u7A7A\u4E3A\u5B9E\u7EBF\u3002\u4EC5\u6298\u7EBF\u7C7B\u578B\u751F\u6548"
    }),
    // area
    lineColor: Schema.optional(Schema.String).annotations({
      title: "\u63CF\u8FB9\u989C\u8272",
      description: "\u9762\u79EF\u56FE\u9876\u90E8\u63CF\u8FB9\u7EBF\u989C\u8272\uFF08CSS \u989C\u8272\u503C\uFF09\uFF1B\u7559\u7A7A\u5219\u4E0E\u586B\u5145\u540C\u8272\u3002\u4EC5\u9762\u79EF\u7C7B\u578B\u751F\u6548"
    }),
    // groupedBar / stackedBar
    width: Schema.optional(numberExpression(configure)).annotations({
      title: "\u67F1\u5BBD",
      description: "\u8FD4\u56DE\u67F1 / \u67F1\u7EC4\u5BBD\u5EA6\uFF08\u50CF\u7D20\uFF09\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u7559\u7A7A\u5219\u81EA\u52A8\u3002\u4EC5\u5206\u7EC4\u67F1\u3001\u5806\u53E0\u67F1\u7C7B\u578B\u751F\u6548"
    }),
    // scatter
    size: Schema.optional(listExpression(configure)).annotations({
      title: "\u70B9\u5927\u5C0F",
      description: "\u8FD4\u56DE\u6BCF\u4E2A\u70B9\u76F4\u5F84\uFF08\u50CF\u7D20\uFF09\u7684\u6570\u5B57\u5217\u8868\uFF0C\u4E0E Y \u6309\u4E0B\u6807\u5BF9\u9F50\uFF1B\u7559\u7A7A\u5219\u7528\u9ED8\u8BA4\u5927\u5C0F\u3002\u4EC5\u6563\u70B9\u7C7B\u578B\u751F\u6548"
    })
  }).annotations({ title: "Y \u7CFB\u5217", description: "\u4E00\u4E2A Y \u7CFB\u5217\uFF0C\u53EF\u72EC\u7ACB\u9009\u62E9\u56FE\u8868\u7C7B\u578B" });
}
export function schema(configure, _blockRef) {
  return Schema.Struct({
    id: Schema.UUID.annotations({ description: "\u5757\u552F\u4E00\u6807\u8BC6" }),
    type: Schema.tag(type),
    compatibilityDate: Schema.tag(compatibilityDate),
    displayName: Schema.optional(Schema.String.annotations({
      title: "\u5185\u90E8\u540D\u79F0",
      description: "\u4EC5\u5728\u7F16\u8F91\u5668\u5185\u53EF\u89C1\u7684\u5757\u540D\uFF0C\u7528\u4E8E\u5728\u4FA7\u8FB9\u680F\u548C\u5E03\u5C40\u7F16\u8F91\u5668\u4E2D\u8BC6\u522B\u5757\uFF1B\u8FD0\u884C\u65F6\u4E0D\u5C55\u793A"
    })),
    caption: Schema.optional(Locale).annotations({
      title: "\u8BF4\u660E\u6587\u5B57",
      description: "\u663E\u793A\u5728\u56FE\u8868\u4E0B\u65B9\u7684\u672C\u5730\u5316\u8BF4\u660E\u6587\u5B57\uFF1B\u7559\u7A7A\u5219\u4E0D\u663E\u793A"
    }),
    dataSources: Schema.Array(dataSourceSchema(configure)).annotations({
      title: "\u6570\u636E\u6E90",
      description: "\u4E00\u4E2A\u6216\u591A\u4E2A\u547D\u540D\u6570\u636E\u6E90\uFF1B\u6BCF\u4E2A\u6570\u636E\u6E90\u7684\u7ED3\u679C\u5217\u8868\u4EE5\u5176\u53D8\u91CF\u540D\u63D0\u4F9B\u7ED9 X / Y \u8868\u8FBE\u5F0F"
    }),
    x: listExpression(configure).annotations({
      title: "X",
      description: '\u8FD4\u56DE\u4E00\u4E2A\u5217\u8868\u4F5C\u4E3A X \u503C\uFF1B\u53EF\u7531 `range` \u751F\u6210\uFF08\u5982 `range(date("2024-01-01"), date("2024-12-01"), "month")`\uFF09\uFF0C\u6216\u5BF9\u6570\u636E\u6E90\u505A `.map`\uFF1B\u5B57\u7B26\u4E32\u5217\u8868\u89C6\u4F5C\u5206\u7C7B\u8F74\uFF0C\u6570\u5B57 / \u65E5\u671F\u5217\u8868\u89C6\u4F5C\u8FDE\u7EED\u8F74'
    }),
    y: Schema.Array(ySeriesSchema(configure)).pipe(Schema.minItems(1)).annotations({
      title: "Y",
      description: "\u6BCF\u4E2A\u7CFB\u5217\u4E00\u6761\u6570\u636E\uFF0C\u6309\u6240\u9009\u56FE\u8868\u7C7B\u578B\u6E32\u67D3\uFF0C\u8FD4\u56DE\u7684\u5217\u8868\u4E0E X \u6309\u4E0B\u6807\u5BF9\u9F50\uFF1B\u81F3\u5C11\u914D\u7F6E\u4E00\u6761"
    })
  }).pipe(
    Schema.filter((value) => {
      const seen = /* @__PURE__ */ new Set();
      for (const ds of value.dataSources) {
        if (seen.has(ds.name)) return `\u6570\u636E\u6E90\u53D8\u91CF\u540D\u91CD\u590D\uFF1A${ds.name}`;
        seen.add(ds.name);
      }
      return true;
    })
  ).annotations({
    title: "XY \u56FE\u8868\u5757",
    description: "\u4ECE\u547D\u540D\u6570\u636E\u6E90\u53D6\u6570\uFF0C\u628A X / Y \u5217\u8868\u6E32\u67D3\u4E3A\u6298\u7EBF / \u9762\u79EF / \u67F1 / \u6563\u70B9\u56FE"
  });
}
export function defaultBody() {
  return {
    dataSources: [],
    x: "[]",
    y: [{
      id: crypto.randomUUID(),
      value: "[]",
      chart: "line",
      curveType: "monotoneX",
      lineWidth: 2
    }]
  };
}
