import { Component, input } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ControlValueAccessor } from '@angular/forms';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { heroChevronDown } from '@ng-icons/heroicons/outline';
import {
  injectSelectState,
  NgpSelect,
  NgpSelectDropdown,
  NgpSelectOption,
  NgpSelectPortal,
} from 'ng-primitives/select';
import { ChangeFn, provideValueAccessor, TouchedFn } from 'ng-primitives/utils';

@Component({
  selector: '<% if (prefix) { %><%= prefix %>-<% } %>select',
  hostDirectives: [
    {
      directive: NgpSelect,
      inputs: ['ngpSelectValue:value', 'ngpSelectMultiple:multiple', 'ngpSelectDisabled:disabled'],
      outputs: ['ngpSelectValueChange:valueChange'],
    },
  ],
  providers: [provideIcons({ heroChevronDown }), provideValueAccessor(Select<%= componentSuffix %>)],
  imports: [NgpSelectDropdown, NgpSelectOption, NgpSelectPortal, NgIcon],
  template: `
    @if (state().value(); as value) {
      <span class="select-value">{{ value }}</span>
    } @else {
      <span class="select-placeholder">{{ placeholder() }}</span>
    }

    <ng-icon name="heroChevronDown" />

    <div *ngpSelectPortal ngpSelectDropdown>
      @for (option of options(); track option) {
        <div [ngpSelectOptionValue]="option" ngpSelectOption>
          {{ option }}
        </div>
      } @empty {
        <div class="empty-message">No options found</div>
      }
    </div>
  `,
  styles: `
/* These styles rely on CSS variables that can be imported from ng-primitives/example-theme/index.css in your global styles */

    :host {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 36px;
      width: 300px;
      border-radius: 8px;
      border: none;
      background-color: var(--ngp-background);
      box-shadow: var(--ngp-input-shadow);
      box-sizing: border-box;
    }

    :host[data-focus] {
      outline: 2px solid var(--ngp-focus-ring);
      outline-offset: 2px;
    }

    .select-value,
    .select-placeholder {
      display: flex;
      align-items: center;
      flex: 1;
      padding: 0 16px;
      background-color: transparent;
      color: var(--ngp-text-primary);
      font-family: inherit;
      font-size: 14px;
      padding: 0 16px;
      height: 100%;
    }

    .select-placeholder {
      color: var(--ngp-text-secondary);
    }

    ng-icon {
      display: inline-flex;
      justify-content: center;
      align-items: center;
      height: 100%;
      margin-inline: 8px;
      font-size: 14px;
    }

    [ngpSelectDropdown] {
      background-color: var(--ngp-background);
      border: 1px solid var(--ngp-border);
      padding: 0.25rem;
      border-radius: 0.75rem;
      outline: none;
      position: absolute;
      animation: popover-show 0.1s ease-out;
      width: var(--ngp-select-width);
      box-shadow: var(--ngp-shadow-lg);
      box-sizing: border-box;
      margin-top: 4px;
      max-height: 240px;
      overflow-y: auto;
      transform-origin: var(--ngp-select-transform-origin);
    }

    [ngpSelectDropdown][data-enter] {
      animation: select-show 0.1s ease-out;
    }

    [ngpSelectDropdown][data-exit] {
      animation: select-hide 0.1s ease-out;
    }

    [ngpSelectOption] {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      padding: 0.375rem 0.75rem;
      cursor: pointer;
      border-radius: 0.5rem;
      width: 100%;
      height: 36px;
      font-size: 14px;
      color: var(--ngp-text-primary);
      box-sizing: border-box;
    }

    [ngpSelectOption][data-hover] {
      background-color: var(--ngp-background-hover);
    }

    [ngpSelectOption][data-press] {
      background-color: var(--ngp-background-active);
    }

    [ngpSelectOption][data-active] {
      background-color: var(--ngp-background-active);
    }

    .empty-message {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 0.5rem;
      color: var(--ngp-text-secondary);
      font-size: 14px;
      font-weight: 500;
      text-align: center;
    }

    @keyframes select-show {
      0% {
        opacity: 0;
        transform: translateY(-10px) scale(0.9);
      }
      100% {
        opacity: 1;
        transform: translateY(0) scale(1);
      }
    }

    @keyframes select-hide {
      0% {
        opacity: 1;
        transform: translateY(0) scale(1);
      }
      100% {
        opacity: 0;
        transform: translateY(-10px) scale(0.9);
      }
    }

    /* Reduce motion for users who prefer reduced motion. */
    @media (prefers-reduced-motion: reduce) {
      [ngpSelectDropdown],
      [ngpSelectDropdown][data-enter],
      [ngpSelectDropdown][data-exit] {
        animation-duration: 0s;
      }
    }
  `,
  host: {
    '[attr.aria-label]': 'ariaLabel() || null',
    '(focusout)': 'onTouched?.()',
  },
})
export class Select<%= componentSuffix %> implements ControlValueAccessor {
  /** Access the underlying select primitive state. */
  protected readonly state = injectSelectState<string>();

  /** The options for the select. */
  readonly options = input<string[]>([]);

  /** The placeholder for the input. */
  readonly placeholder = input<string>('');

  /** The accessible label for the select trigger. */
  readonly ariaLabel = input<string>('');

  /** Form change callback. */
  private onChange?: ChangeFn<string | undefined>;

  /** Form touched callback. */
  protected onTouched?: TouchedFn;

  constructor() {
    // Forward primitive value changes (user-driven selections) to the form.
    this.state()
      .valueChange.pipe(takeUntilDestroyed())
      .subscribe(value => this.onChange?.(value as string | undefined));
  }

  writeValue(value: string | undefined): void {
    // Pass { emit: false } so writeValue doesn't bounce back through onChange.
    this.state().setValue(value, { emit: false });
  }

  registerOnChange(fn: ChangeFn<string | undefined>): void {
    this.onChange = fn;
  }

  registerOnTouched(fn: TouchedFn): void {
    this.onTouched = fn;
  }

  setDisabledState(isDisabled: boolean): void {
    this.state().setDisabled(isDisabled);
  }
}
