import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
} from '@angular/core';
import { isaFilialeLocation } from '@isa/icons';
import { NgIconComponent, provideIcons } from '@ng-icons/core';

/**
 * Displays meta information about a product shelf, including department, shelf label,
 * product group, assortment, and return reason. Designed for use with ReturnItem or
 * ReturnSuggestion data structures.
 *
 * @remarks
 * - Uses Angular signals for local state.
 * - Follows OnPush change detection for performance.
 * - Provides icon support via NgIconComponent.
 *
 * @example
 * <remi-product-shelf-meta-info
 *   [department]="item.department"
 *   [shelfLabel]="item.shelfLabel"
 *   [productGroupKey]="item.productGroup"
 *   [productGroupValue]="productGroupValue"
 *   [assortment]="item.assortment"
 *   [returnReason]="item.returnReason"
 * ></remi-product-shelf-meta-info>
 */
@Component({
  selector: 'remi-product-shelf-meta-info',
  templateUrl: './product-shelf-meta-info.component.html',
  styleUrls: ['./product-shelf-meta-info.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [NgIconComponent],
  providers: [provideIcons({ isaFilialeLocation })],
})
export class ProductShelfMetaInfoComponent {
  /**
   * Department name (e.g., "Reise").
   * Sourced from ReturnItem or ReturnSuggestion.
   */
  department = input<string>('');

  /**
   * Shelf label (e.g., "Europa").
   * Sourced from ReturnItem or ReturnSuggestion.
   */
  shelfLabel = input<string>('');

  /**
   * Product group key.
   * Sourced from ReturnItem or ReturnSuggestion (product.productGroup).
   */
  productGroupKey = input<string>('');

  /**
   * Product group value.
   * Sourced from fetchProductGroups (Key/Value).
   */
  productGroupValue = input<string>('');

  /**
   * Assortment string, possibly delimited (see {@link assortmentFormatted}).
   * Sourced from ReturnItem or ReturnSuggestion.
   */
  assortment = input<string>('');

  /**
   * Return reason.
   * Sourced from ReturnItem or ReturnSuggestion.
   */
  returnReason = input<string>('');

  /**
   * Computed shelf information string, combining department and shelf label.
   * Returns a string in the format "department / shelfLabel", omitting empty values.
   *
   * @returns {string} Formatted shelf info or empty string.
   */
  shelfInfos = computed(() => {
    const [department, shelfLabel] = [this.department(), this.shelfLabel()];
    return [department, shelfLabel].filter(Boolean).join(' / ');
  });

  /**
   * Computed product group string, combining key and value.
   * Returns a string in the format "key: value", omitting empty values.
   *
   * @returns {string} Formatted product group or empty string.
   */
  productGroup = computed(() => {
    const [key, value] = [this.productGroupKey(), this.productGroupValue()];
    return [key, value].filter(Boolean).join(': ');
  });

  /**
   * Formats an assortment string like "Label1|Key1Label2|Key2..."
   * into "Key1: Label1, Key2: Label2". Returns raw value if no delimiter is present.
   *
   * @example
   * // "Basissortiment|BPrämienartikel|n" → "B: Basissortiment, n: Prämienartikel"
   * // "Archivartikel|C" → "C: Archivartikel"
   * // "Basissortiment" → "Basissortiment"
   *
   * @returns {string} Formatted assortment string or raw value.
   */
  assortmentFormatted = computed(() => {
    const raw = this.assortment();
    if (!raw) return '';
    const parts = raw.split('|');
    if (parts.length === 1) return raw;

    const result: string[] = [];
    for (let i = 0; i < parts.length - 1; i++) {
      const label = parts[i];
      const next = parts[i + 1];
      const key = next[0];
      result.push(`${key}: ${label}`);
      parts[i + 1] = next.slice(1);
    }
    return result.filter(Boolean).join(', ');
  });
}
