import { CustomerAttributeSave, CustomerAttribute } from '@common/customerAttributes/types.js';
import { Generation } from '@api-consent/types/common.js';
import { ConsentGroup } from '@src/types/comsApi.js';

type StandardEvent = Event;

export interface PageDetails {
  pageFooter?: string;
}

export type ModifiedState = 'granted' | 'revoked';

export type ApiLoadConsentResponse = {
  groups: ConsentGroup[];
  details: PageDetails;
  customerAttributes: CustomerAttribute[];
  generation: Generation;
};

export namespace TefComs {

  export interface MicroFrontend extends HTMLElement, ConfigOptions {
    save: () => Promise<void>;
    cancel: () => void;
    getConsents: () => ShortConsent[];
    getConsentStatus: () => ConsentStatus[];
    addEventListener<T extends keyof EventMap>(
      type: T,
      listener: (this: MicroFrontend, ev: EventMap[T]) => unknown,
      options?: boolean | AddEventListenerOptions
    ): void;
    // the fallback for any event names not in EventMap
    addEventListener(
      type: string,
      listener: (this: MicroFrontend, ev: StandardEvent) => unknown,
      options?: boolean | AddEventListenerOptions
    ): void;
  }

  export interface ConfigOptions {
    [key: string | boolean | string[] | number | undefined];

    apiUri: string;
    agentToken?: string;
    gatewayToken?: string;
    lifecycle: string;
    mode?: string;
    traceid?: string;
    occlUri: string,
    styleContext?: string;
    consentwhitelist?: string;
    channel?: string;
    usertype?: string;
    vonr?: string;
    references?: string;
    showReferences?: boolean;
    showVersions?: boolean;
    isExistingRegister?: boolean;
    spId?: string;
    lcId?: string;
    registerId?: string;
    fetch?: string;
    isNewCustomer?: boolean;
    groups?: string[];
    group?: string;
    generation?: number;
    sessionkey?: string;
  }

  export interface Reference {
    type: string;
    source: string;
    id: string;
    state?: 'inEdit' | 'new';
  }

  export interface ShortConsent {
    id: string;
    granted: boolean;
    shortInfo: string; // html-formatted
    version: string;
    channels?: ShortFeatureValue[];
    dataTypes?: ShortFeatureValue[];
    productTypes?: ShortFeatureValue[];
    purposeTypes?: ShortFeatureValue[];
  }

  export interface ConsentStatus {
    type?: string;
    id: string;
    granted: boolean;
    channels?: ShortFeatureValue[];
    dataTypes?: ShortFeatureValue[];
    productTypes?: ShortFeatureValue[];
    purposeTypes?: ShortFeatureValue[];
  }

  export type ConsentAndCustomerAttributeSaveType = ConsentStatus | CustomerAttributeSave;

  export interface ShortFeatureValue {
    id: string;
    granted: boolean;
  }

  export interface EventDetails {
    name: string;
    level: 'error' | 'info';
    message?: string;
    error?: Error;
    data?: Record<string, unknown>;
  }

  export type Event = CustomEvent<EventDetails>;

  interface EventMap {

    'tef-coms4cs': Event;
  }
}

declare global {
  interface HTMLElementTagNameMap {
    'tef-coms4cs': TefComs.MicroFrontend;
  }
  interface DocumentEventMap {
    'tef-coms4cs': TefComs.Event;
  }
  interface WindowEventMap {
    'tef-coms4cs': TefComs.Event;
  }
}
