import { Schema } from "effect";
import { Expression, HttpRequestResult, LocaleMarkdown } from "../../../../../share/expression.js";
export { getStructFieldDescription, getStructFieldTitle } from "../../../../table/utils/schema-meta.js";
export const type = "com.shwfed.block.animated.number";
export const compatibilityDate = "2026-06-01";
export const metadata = {
  name: "\u52A8\u753B\u6570\u5B57",
  icon: "fluent:number-symbol-20-regular",
  w: { initial: 8, min: 2, max: Infinity },
  h: { initial: 3, min: 2, max: Infinity }
};
function registerJson(env) {
  env.registerVariable("json", "optional<dyn>", {
    label: "HTTP \u54CD\u5E94\u4F53",
    description: "HTTP \u54CD\u5E94\u4F53\uFF08\u914D\u7F6E\u4E86\u8BF7\u6C42\u65F6\u4E3A\u5DF2\u89E3\u6790 JSON\uFF0C\u5426\u5219\u4E3A none\uFF09"
  });
}
export function dataSourceSchema(configure) {
  const CelRequest = Expression({
    configure,
    resultType: HttpRequestResult
  });
  const CelNumber = Expression({
    configure: (env) => {
      configure(env);
      registerJson(env);
    },
    // Tolerate `dyn` / `optional` like `tableDataSource`'s total — most
    // response-derived expressions check as `dyn`, narrowing only at runtime.
    resultType: (t) => t === "number" || t === "dyn" || t.startsWith("optional")
  });
  return Schema.Struct({
    request: Schema.optional(CelRequest).annotations({
      title: "\u8BF7\u6C42",
      description: "\u53EF\u9009\u7684 HTTP \u8BF7\u6C42\u8868\u8FBE\u5F0F\uFF1B\u672A\u914D\u7F6E\u65F6\u6570\u503C\u76F4\u63A5\u4ECE `\u6570\u503C` \u8868\u8FBE\u5F0F\u8BFB\u53D6\uFF08\u901A\u5E38\u5F15\u7528\u4E0A\u4E0B\u6587\u53D8\u91CF\uFF09"
    }),
    value: CelNumber.annotations({
      title: "\u6570\u503C",
      description: "\u8FD4\u56DE\u8981\u5C55\u793A\u7684\u6570\u5B57\uFF08`number`\uFF09\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u914D\u7F6E\u4E86 `\u8BF7\u6C42` \u65F6\u53EF\u901A\u8FC7 `json` \u5F15\u7528\u54CD\u5E94\u4F53"
    })
  }).annotations({
    title: "\u6570\u636E\u6E90",
    description: "\u4E00\u4E2A HTTP \u8BF7\u6C42\u52A0\u4E00\u4E2A\u8FD4\u56DE\u6570\u5B57\u7684\u8868\u8FBE\u5F0F"
  });
}
export function itemSchema(configure) {
  const CelString = Expression({
    configure: (env) => {
      configure(env);
      registerJson(env);
    },
    // Tolerate `dyn` / `optional` so a `json`-derived string (which checks as
    // `dyn`) passes validation, mirroring the `value` field's tolerance.
    resultType: (t) => t === "string" || t === "dyn" || t.startsWith("optional")
  });
  const CelStyle = Expression({
    configure: (env) => {
      configure(env);
      registerJson(env);
    },
    resultType: (t) => t === "string" || t === "dyn" || t.startsWith("map") || t.startsWith("optional")
  });
  return Schema.Struct({
    id: Schema.UUID.annotations({ description: "\u6570\u5B57\u9879\u552F\u4E00\u6807\u8BC6" }),
    name: Schema.optional(Schema.String.annotations({
      title: "\u5185\u90E8\u540D\u79F0",
      description: "\u4EC5\u5728\u7F16\u8F91\u5668\u5185\u53EF\u89C1\u7684\u6570\u5B57\u9879\u540D\uFF0C\u7528\u4F5C\u4FA7\u8FB9\u680F\u6807\u7B7E\uFF1B\u8FD0\u884C\u65F6\u4E0D\u5C55\u793A"
    })),
    dataSource: dataSourceSchema(configure),
    pollingInterval: Schema.optional(
      Schema.Number.pipe(Schema.int(), Schema.positive()).annotations({
        title: "\u8F6E\u8BE2\u95F4\u9694",
        description: "\u8F6E\u8BE2\u5237\u65B0\u95F4\u9694\uFF08\u79D2\uFF09\uFF0C\u6B63\u6574\u6570\uFF1B\u7559\u7A7A\u5219\u4E0D\u8F6E\u8BE2\u3002\u7A97\u53E3\u5931\u7126\u65F6\u6682\u505C\uFF0C\u91CD\u65B0\u805A\u7126\u65F6\u7ACB\u5373\u5237\u65B0\u5E76\u6062\u590D\u8F6E\u8BE2"
      })
    ),
    prefix: Schema.optional(CelString.annotations({
      title: "\u524D\u7F00",
      description: "\u8FD4\u56DE\u6570\u5B57\u524D\u5C55\u793A\u6587\u672C\uFF08`string`\uFF09\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u53EF\u5F15\u7528 `json`\u3002\u7559\u7A7A\u5219\u4E0D\u5C55\u793A"
    })),
    suffix: Schema.optional(CelString.annotations({
      title: "\u540E\u7F00",
      description: "\u8FD4\u56DE\u6570\u5B57\u540E\u5C55\u793A\u6587\u672C\uFF08`string`\uFF09\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u53EF\u5F15\u7528 `json`\u3002\u7559\u7A7A\u5219\u4E0D\u5C55\u793A"
    })),
    style: Schema.optional(CelStyle.annotations({
      title: "\u6837\u5F0F",
      description: "\u8FD4\u56DE\u5E94\u7528\u5728\u8BE5\u6570\u5B57\u4E0A\u7684\u6837\u5F0F\u7684 CEL \u8868\u8FBE\u5F0F\uFF0C\u53EF\u8FD4\u56DE CSS \u5B57\u7B26\u4E32\uFF08\u5982 `'font-size: 3rem'`\uFF09\u6216\u6837\u5F0F\u5BF9\u8C61\uFF08\u5982 `{'fontSize': '3rem'}`\uFF09\uFF1B\u53EF\u5F15\u7528 `json`\u3002\u7559\u7A7A\u5219\u4E0D\u8BBE\u6837\u5F0F"
    }))
  }).annotations({
    title: "\u6570\u5B57\u9879",
    description: "\u4E00\u4E2A\u72EC\u7ACB\u53D6\u6570\u3001\u53EF\u8F6E\u8BE2\u7684\u52A8\u753B\u6570\u5B57"
  });
}
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"
    })),
    locale: Schema.optional(LocaleMarkdown({ configure }).annotations({
      title: "\u8BF4\u660E\u6587\u5B57",
      description: "\u663E\u793A\u5728\u6570\u5B57\u4E0B\u65B9\u7684\u672C\u5730\u5316\u8BF4\u660E\u6587\u5B57\uFF0C\u6309\u5185\u8054 Markdown \u6E32\u67D3\uFF0C\u652F\u6301 `{{ \u8868\u8FBE\u5F0F }}` CEL \u63D2\u503C\uFF1B\u7559\u7A7A\u5219\u4E0D\u663E\u793A"
    })),
    items: Schema.Array(itemSchema(configure)).pipe(Schema.minItems(1)).annotations({
      title: "\u6570\u5B57\u9879",
      description: "\u4E00\u4E2A\u6216\u591A\u4E2A\u52A8\u753B\u6570\u5B57\uFF0C\u5E76\u6392\u5C55\u793A\u5E76\u5171\u4EAB\u52A8\u753B\u5206\u7EC4\uFF1B\u81F3\u5C11\u914D\u7F6E\u4E00\u4E2A"
    })
  }).annotations({
    title: "\u52A8\u753B\u6570\u5B57\u5757",
    description: "\u628A\u4E00\u4E2A\u6216\u591A\u4E2A\u6570\u636E\u6E90\u5404\u53D6\u4E00\u4E2A\u6570\u5B57\uFF0C\u4EE5\u52A8\u753B\u8FC7\u6E21\u5E76\u6392\u6E32\u67D3\uFF0C\u53EF\u9009\u9010\u9879\u8F6E\u8BE2"
  });
}
export function defaultBody() {
  return { items: [{ id: crypto.randomUUID(), dataSource: { value: "0" } }] };
}
