import { styleProps } from "./Input";

export default {
  name: "Date Input",
  acceptsChildren: false,
  category: "Input",
  styleProps,
  props: [
    {
      label: "Initial Value",
      name: "defaultValue",
      type: { kind: "date", isDynamic: true },
      isDefault: true,
      default: "",
      placeholder: "Enter a date...",
      help: "The input's default value",
    },
    {
      label: "Placeholder",
      name: "placeholder",
      type: { kind: "text", isDynamic: true },
      isDefault: true,
      default: "Enter a date...",
      placeholder: "Enter a placeholder...",
      help: "The placeholder text to display",
    },
    {
      label: "Format",
      name: "type",
      type: { kind: "select" },
      default: "date",
      isDefault: true,
      options: [
        { label: "Date", value: "date" },
        { label: "Time", value: "time" },
        { label: "Date and Time", value: "datetime-local" },
      ],
      placeholder: "",
      help: "The type of input to use",
    },
    {
      label: "Required",
      name: "required",
      isDefault: true,
      type: { kind: "boolean" },
      default: false,
      placeholder: "",
      help: "The is this value required by a form?",
    },
    {
      label: "Is disabled",
      name: "disabled",
      type: { kind: "boolean", isDynamic: true },
      default: false,
      placeholder: "",
      help: "Whether the input is disabled",
    },
  ],
  events: [
    {
      label: "When changed",
      name: "onChange",
      isDefault: true,
      offerDebounce: true,
      help: "Select an action to run when input's value has changed",
    },
    {
      label: "When focused",
      name: "onFocus",
      help: "Select an action to run when input's has focused",
    },
    {
      label: "When blurred",
      name: "onBlur",
      help: "Select an action to run when input's has blurred",
    },
  ],
  defaultStyles: {
    ":focus-visible": {
      "outline-width": "1px",
      "outline-style": "solid",
      "outline-color": "var(--palette-ring)",
    },
    ":user-invalid": {
      "border-color": "var(--theme-border-error)",
    },
    ":disabled": {
      opacity: 0.5,
      cursor: "not-allowed",
    },
    "": {
      width: "100%",
      "min-width": "0",
      outline: "var(--theme-sizes-zero)",
      position: "relative",
      "text-align": "left",
      "border-radius": "var(--theme-radii-md)",
      "font-family": "var(--theme-fonts-body)",
      display: "inline-flex",
    },
  },
  sources: [
    {
      id: "value",
      name: "{{= it.component.name }}'s current value",
      provider: "StateProvider",
      description: "The current value of the input",
      template: "$get('{{= it.component.id}}', { value: null }).value",
      instanceTemplate: `{{= it.component.name }}'s current value`,
      outputType: { kind: "date" },
    },
    {
      id: "isValid",
      provider: "StateProvider",
      name: "{{= it.component.name }}'s value is valid",
      description: "Returns true if input value is valid",
      template: `document.querySelector('[data-id|="{{= it.component.id }}"]')?.checkValidity();`,
      instanceTemplate: `is {{= it.component.name }}'s value valid`,
      outputType: { kind: "boolean" },
    },
  ],
  actions: [
    {
      id: "set",
      name: `Set "{{= it.component.name }}"'s value`,
      provider: "StateProvider",
      description: "Set the value of the state variable",
      async: false,
      template:
        // eslint-disable-next-line no-template-curly-in-string
        "$set('{{= it.component.id }}', { value: {{= it.stringify(it.data.value) }} });",
      instanceTemplate: `{{= it.stringify(it.data.value) }}`,
      props: [
        {
          label: "Value",
          name: "value",
          type: { kind: "date", isDynamic: true },
          placeholder: "Choose a value",
          required: true,
          default: null,
          help: "The value to set the state variable to",
        },
      ],
    },
    {
      id: "focus",
      name: `Focus "{{= it.component.name }}" input`,
      description: "Focus the input",
      async: false,
      template:
        // eslint-disable-next-line no-template-curly-in-string
        'document.querySelector(`[data-id="${$$getKey({{= it.stringify(it.component.id) }})}"]`)?.focus();',
      instanceTemplate: ``,
      props: [],
    },
    {
      id: "blur",
      name: `Blur "{{= it.component.name }}" input`,
      description: "Blur the input",
      async: false,
      template:
        // eslint-disable-next-line no-template-curly-in-string
        'document.querySelector(`[data-id="${$$getKey({{= it.stringify(it.component.id) }})}"]`)?.blur();',
      instanceTemplate: `Blur input "{{= it.component.name }}"`,
      props: [],
    },
  ],
} as const;
