import { Schema } from "effect";
import { Locale } from "../../../../../share/locale.js";
import { Slot, defaultSlot } from "../../../../../share/layout.js";
export { getStructFieldDescription, getStructFieldTitle } from "../../../../table/utils/schema-meta.js";
export const type = "com.shwfed.block.tabs";
export const compatibilityDate = "2026-05-06";
export const metadata = {
  name: "\u6807\u7B7E\u9875",
  icon: "fluent:tabs-20-regular",
  w: { initial: 12, min: 4, max: Infinity },
  h: { initial: 6, min: 4, max: Infinity }
};
export const TabPaneMeta = Schema.Struct({
  label: Locale.annotations({
    title: "\u6807\u7B7E\u540D",
    description: "\u8BE5\u6807\u7B7E\u9875\u7684\u672C\u5730\u5316\u663E\u793A\u540D"
  })
});
const MAX_NESTING_DEPTH = 8;
function tabsNestingExceeds(value, maxDepth, currentDepth = 0) {
  if (currentDepth > maxDepth) return true;
  if (value === null || typeof value !== "object") return false;
  if (Array.isArray(value)) {
    return value.some((v) => tabsNestingExceeds(v, maxDepth, currentDepth));
  }
  const o = value;
  if (o.type === type && Array.isArray(o.tabs)) {
    return o.tabs.some((t) => {
      if (!t || typeof t !== "object") return false;
      const slot = t.slot;
      return tabsNestingExceeds(slot, maxDepth, currentDepth + 1);
    });
  }
  return Object.values(o).some((v) => tabsNestingExceeds(v, maxDepth, currentDepth));
}
export function schema(configure, blockRef) {
  const TabsSlot = Slot(blockRef, configure);
  const TabPane = Schema.Struct({
    id: Schema.UUID.annotations({ description: "\u6807\u7B7E\u9875\u552F\u4E00\u6807\u8BC6" }),
    ...TabPaneMeta.fields,
    slot: TabsSlot.annotations({
      title: "\u5185\u5BB9",
      description: "\u6807\u7B7E\u9875\u5185\u90E8\u7684\u5757\u4E0E\u5E03\u5C40"
    })
  }).annotations({
    title: "TabPane",
    description: "\u5355\u4E2A\u6807\u7B7E\u9875\uFF1A\u540D\u79F0 + \u5185\u5BB9 Slot"
  });
  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"
    })),
    style: Schema.optional(Schema.String).annotations({
      title: "\u5BB9\u5668\u6837\u5F0F",
      description: "\u5E94\u7528\u5728\u6807\u7B7E\u9875\u6700\u5916\u5C42\u5BB9\u5668\u4E0A\u7684 CSS \u6837\u5F0F\u5B57\u7B26\u4E32"
    }),
    tabs: Schema.Array(TabPane).pipe(
      Schema.minItems(1),
      Schema.filter((tabs) => {
        const ids = /* @__PURE__ */ new Set();
        for (const t of tabs) {
          if (ids.has(t.id)) return `\u6807\u7B7E\u9875 id \u91CD\u590D: ${t.id}`;
          ids.add(t.id);
        }
        return true;
      })
    ).annotations({
      title: "\u6807\u7B7E\u9875",
      description: "\u81F3\u5C11 1 \u9879\uFF0C\u6309\u987A\u5E8F\u6E32\u67D3"
    })
  }).pipe(Schema.filter((value) => {
    if (tabsNestingExceeds(value, MAX_NESTING_DEPTH)) {
      return `\u6807\u7B7E\u9875\u5D4C\u5957\u6DF1\u5EA6\u8D85\u8FC7 ${MAX_NESTING_DEPTH} \u5C42`;
    }
    return true;
  })).annotations({
    title: "\u6807\u7B7E\u9875\u5757",
    description: "\u5C06\u4E00\u7EC4\u5E26\u6807\u7B7E\u7684\u5B50 Slot \u4F5C\u4E3A\u9875\u9762\u5757\u5D4C\u5165\uFF1B\u652F\u6301\u4EFB\u610F\u5C42\u7EA7\u5D4C\u5957"
  });
}
function makeId() {
  if (typeof crypto !== "undefined" && "randomUUID" in crypto) return crypto.randomUUID();
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
    const r = Math.random() * 16 | 0;
    return (c === "x" ? r : r & 3 | 8).toString(16);
  });
}
export function defaultBody() {
  return {
    tabs: [
      {
        id: makeId(),
        label: [{ locale: "zh", message: "\u6807\u7B7E 1" }],
        slot: defaultSlot()
      }
    ]
  };
}
