<script setup>
import { computed } from "vue";
import { Icon } from "@iconify/vue";
import { ExpressionEditor } from "../../../../ui/expression-editor";
import { Separator } from "../../../../ui/separator";
import { Field, FieldLabel } from "../../../../ui/field";
import { Locale } from "../../../../ui/locale";
import {
  InputGroup,
  InputGroupAddon,
  InputGroupButton,
  InputGroupInput,
  InputGroupNumberField
} from "../../../../ui/input-group";
import { getStructFieldDescription, getStructFieldTitle } from "../../../utils/schema-meta";
import { Markdown } from "../../../../ui/markdown";
import DerivedValueEditor from "../../../../form/DerivedValueEditor.vue";
import { schema } from "./schema";
defineOptions({ name: "ShwfedTableTextInputRendererConfig" });
const value = defineModel({ type: Object, ...{ required: true } });
const fieldSchema = schema(() => {
});
const fieldTitle = (field) => getStructFieldTitle(fieldSchema, field) ?? field;
const fieldDescription = (field) => getStructFieldDescription(fieldSchema, field);
const ROW_VARS = {
  row: { type: "dyn", label: "\u5F53\u524D\u884C\u6570\u636E" },
  index: { type: "number", label: "\u884C\u7D22\u5F15" }
};
const bindingText = computed({
  get: () => value.value.binding ?? "",
  set: (v) => {
    const trimmed = v.trim();
    if (trimmed === "") {
      const { binding: _drop, ...rest } = value.value;
      value.value = rest;
    } else {
      value.value = { ...value.value, binding: trimmed };
    }
  }
});
const hiddenModel = computed({
  get: () => value.value.hidden ?? "",
  set: (v) => {
    if (v === "") {
      const { hidden: _drop, ...rest } = value.value;
      value.value = rest;
    } else {
      value.value = { ...value.value, hidden: v };
    }
  }
});
const disabledModel = computed({
  get: () => value.value.disabled ?? "",
  set: (v) => {
    if (v === "") {
      const { disabled: _drop, ...rest } = value.value;
      value.value = rest;
    } else {
      value.value = { ...value.value, disabled: v };
    }
  }
});
const readonlyModel = computed({
  get: () => value.value.readonly ?? "",
  set: (v) => {
    if (v === "") {
      const { readonly: _drop, ...rest } = value.value;
      value.value = rest;
    } else {
      value.value = { ...value.value, readonly: v };
    }
  }
});
const derivedModel = computed({
  get: () => value.value.derived,
  set: (v) => {
    if (v == null) {
      const { derived: _drop, ...rest } = value.value;
      value.value = rest;
    } else {
      value.value = { ...value.value, derived: v };
    }
  }
});
</script>

<template>
  <div class="space-y-5">
    <div class="grid grid-cols-2 gap-x-6 gap-y-4">
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('title')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('title')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("title") }}
        </FieldLabel>
        <Locale
          :model-value="value.title"
          @update:model-value="(v) => value = { ...value, title: v }"
        />
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('tooltip')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('tooltip')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("tooltip") }}
        </FieldLabel>
        <Locale
          :model-value="value.tooltip"
          markdown
          @update:model-value="(v) => value = { ...value, tooltip: v }"
        />
      </Field>
    </div>
    <div class="grid grid-cols-2 gap-x-6 gap-y-4">
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('binding')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('binding')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("binding") }}
        </FieldLabel>
        <InputGroup>
          <InputGroupInput
            v-model="bindingText"
            placeholder="例：name"
            class="font-mono"
          />
        </InputGroup>
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('placeholder')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('placeholder')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("placeholder") }}
        </FieldLabel>
        <Locale
          :model-value="value.placeholder"
          @update:model-value="(v) => value = { ...value, placeholder: v }"
        />
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('size')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('size')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("size") }}
        </FieldLabel>
        <InputGroup>
          <InputGroupNumberField
            :model-value="value.size"
            :disabled="value.grow"
            :min="0"
            @update:model-value="(v) => value = { ...value, size: v }"
          />
          <InputGroupAddon align="inline-end">
            <InputGroupButton
              :variant="value.grow ? 'primary' : 'ghost'"
              size="xs"
              @click="value = { ...value, grow: !value.grow }"
            >
              <Icon :icon="value.grow ? 'fluent:lock-closed-20-regular' : 'fluent:arrow-autofit-width-20-regular'" />
              {{ fieldTitle("grow") }}
            </InputGroupButton>
          </InputGroupAddon>
        </InputGroup>
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('hidden')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('hidden')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("hidden") }}
        </FieldLabel>
        <ExpressionEditor
          v-model="hiddenModel"
          placeholder="例：row.archived"
          result-type="bool"
          :extra-vars="ROW_VARS"
        />
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('disabled')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('disabled')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("disabled") }}
        </FieldLabel>
        <ExpressionEditor
          v-model="disabledModel"
          placeholder="例：row.locked"
          result-type="bool"
          :extra-vars="ROW_VARS"
        />
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('readonly')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('readonly')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("readonly") }}
        </FieldLabel>
        <ExpressionEditor
          v-model="readonlyModel"
          placeholder="例：row.id != null"
          result-type="bool"
          :extra-vars="ROW_VARS"
        />
      </Field>
      <Field
        orientation="vertical"
        class="col-span-2"
      >
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('derived')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('derived')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("derived") }}
        </FieldLabel>
        <DerivedValueEditor
          v-model="derivedModel"
          result-type="string"
          placeholder="例：string(row.first) + ' ' + string(row.last)"
        />
      </Field>
    </div>
    <Separator />
    <div class="flex flex-wrap gap-x-8 gap-y-3">
      <Field
        orientation="vertical"
        class="w-full max-w-xs"
      >
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('enableSorting')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('enableSorting')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("enableSorting") }}
        </FieldLabel>
        <InputGroup>
          <InputGroupInput
            :model-value="value.sortKey"
            placeholder="例：created_at"
            @update:model-value="(v) => value = { ...value, sortKey: v || void 0 }"
          />
          <InputGroupAddon align="inline-end">
            <InputGroupButton
              :variant="value.enableSorting ? 'primary' : 'ghost'"
              size="xs"
              @click="value = { ...value, enableSorting: !value.enableSorting }"
            >
              <Icon :icon="value.enableSorting ? 'fluent:arrow-sort-up-16-regular' : 'fluent:arrow-sort-16-regular'" />
              {{ fieldTitle("enableSorting") }}
            </InputGroupButton>
          </InputGroupAddon>
        </InputGroup>
      </Field>
    </div>
  </div>
</template>
