import { graphql } from "@/gql";

export type PigName =
  | "logo"
  | "music"
  | "drink"
  | "dance"
  | "point"
  | "student"
  | "listen"
  | "guard"
  | "key"
  | "chill"
  | "peek";

export const PIG_NAMES = [
  "logo",
  "music",
  "drink",
  "dance",
  "point",
  "student",
  "listen",
  "guard",
  "key",
  "chill",
  "peek",
];

export function getSearchPath(query: string): string {
  const params = new URLSearchParams();
  if (query) {
    params.set("q", query);
  } else {
    params.delete("q");
  }
  return `/sok?${params.toString()}`;
}

export function randomElement(array: any[]): any | undefined {
  return array.length
    ? array[Math.floor(Math.random() * array.length)]
    : undefined;
}

export function unique<T>(array: any[]): any[] {
  return Array.from(array.reduce((set, item) => set.add(item), new Set()));
}

export function stripWhitespace(s: string): string {
  return s.replace(/\s/g, "");
}

export function stripHtml(s: string): string {
  return s.replace(/(<([^>]+)>)/gi, "");
}

export function formatPhoneE164(phone: string): string {
  phone = stripWhitespace(phone);
  if (phone.startsWith("+") || phone.length != 8) {
    return phone;
  }
  return "+47" + phone;
}

export function formatNorwegianPhoneNumber(phone: string): string {
  if (phone.startsWith("+47")) {
    const local = phone.substring(3);
    if (local.length === 8) {
      return `${local.substring(0, 3)} ${local.substring(
        3,
        5
      )} ${local.substring(5)}`;
    }
  }
  return phone;
}

export function formatHumanReadableList(array: (string | number)[]): string {
  const length = array.length;

  if (length === 0) {
    return "";
  }
  if (length === 1) {
    return array[0].toString();
  }
  if (length === 2) {
    return array.join(" og ");
  }
  return array.slice(0, -1).join(", ") + " og " + array[length - 1];
}

const LeafBlocksFragmentDefinition = graphql(`
  fragment LeafBlocks on StreamFieldInterface {
    id
    blockType
    field
    ... on RichTextBlock {
      ...RichTextBlock
    }
    ... on ImageWithTextBlock {
      ...ImageWithTextBlock
    }
    ... on ImageSliderBlock {
      ...ImageSliderBlock
    }
    ... on HorizontalRuleBlock {
      ...HorizontalRuleBlock
    }
    ... on FeaturedBlock {
      ...FeaturedBlock
    }
    ... on ContactListBlock {
      ...ContactListBlock
    }
    ... on EmbedBlock {
      ...EmbedBlock
    }
    ... on FactBoxBlock {
      ...FactBoxBlock
    }
    ... on ScheduleBlock {
      ...ScheduleBlock
    }
  }
`);

const OneLevelOfBlocksFragmentDefinition = graphql(`
  fragment OneLevelOfBlocks on StreamFieldInterface {
    ...LeafBlocks
    ... on AccordionBlock {
      ...AccordionBlock
      body {
        ...LeafBlocks
      }
    }
    ... on PageSectionBlock {
      ...PageSectionBlock
      body {
        ...LeafBlocks
      }
    }
  }
`);

const BlockFragmentDefinition = graphql(`
  fragment Blocks on StreamFieldInterface {
    ...OneLevelOfBlocks
    ... on AccordionBlock {
      ...AccordionBlock
      body {
        ...OneLevelOfBlocks
      }
    }
    ... on PageSectionBlock {
      ...PageSectionBlock
      body {
        ...OneLevelOfBlocks
      }
    }
    ... on ContactSectionBlock {
      ...ContactSectionBlock
      blocks {
        ...OneLevelOfBlocks
        ... on ContactSubsectionBlock {
          ...ContactSubsectionBlock
          blocks {
            ...OneLevelOfBlocks
          }
        }
      }
    }
  }
`);

export const ImageFragmentDefinition = graphql(`
  fragment Image on CustomImage {
    id
    url
    width
    height
    alt
    attribution
  }
`);

export const ContactEntityFragmentDefinition = graphql(`
  fragment ContactEntity on ContactEntity {
    id
    name
    contactType
    title
    email
    phoneNumber
    image {
      ...Image
    }
  }
`);
