import { configVersion } from 'src/types/config-versions';

const getMerchantFrontendConfigQuery = () => `
  query ($version: "${configVersion()}") {
    me {
      getProductsConfigByVersion(version: $version) {
        frontend {
          values
        }
      }
    }
  }
`;

type MountingConfigQueryData = {
  mounting_config_id?: string;
  container_id?: string;
  check_id?: string;
  parent_insertion_point?: {
    selector?: string;
    selection_index?: number;
    insertion_type?: string;
  };
  insertion_point?: {
    selector?: string;
    selection_index?: number;
    insertion_type?: string;
  };
  grid_insertion_type?: string;
  wrapping_element?: {
    class_name?: string;
    element?: string;
    insertion_type?: string;
  };
  div_check_selector?: string;
  suppression_selector?: string;
  style?: Record<string, string | number>;
  class_name?: string;
  data_attributes?: Record<string, string>;
};

type WidgetConfigQueryData = {
  widget_config_id: string;
  type: string;
  content_id?: string;
  // Widget-specific configuration fields will be stored as generic Record
  // since each widget type has different configuration structure
  [key: string]: unknown;
};

type PageVariantCheckQueryData = {
  check_type: string;
  [key: string]: unknown;
};

type WidgetMountingQueryData = {
  mounting_config_id: string;
  widget_config_id?: string;
  mounting_point_widgets?: Array<{
    checks: PageVariantCheckQueryData[];
    widget_config_id: string;
  }>;
};

type PageVariantQueryData = {
  variant_id: string;
  variant_type: string;
  variant_checks: PageVariantCheckQueryData[];
  widget_mounting: WidgetMountingQueryData[];
  plp_id_extractor?: string;
  product_id_extractor?: string;
  parent_product_id_extractor?: string;
};

type UIConfigQueryData = {
  shortName: string;
  [key: string]: unknown;
};

interface GetMerchantFrontendConfigQueryData {
  merchant_override_css?: string;
  page_variants?: PageVariantQueryData[];
  mounting_configs?: {
    key: string;
    config: MountingConfigQueryData;
  }[];
  widget_configs?: {
    key: string;
    config: WidgetConfigQueryData;
  }[];
  ui_configs?: UIConfigQueryData;
}

export { getMerchantFrontendConfigQuery };
export type {
  GetMerchantFrontendConfigQueryData,
  MountingConfigQueryData,
  WidgetConfigQueryData,
  PageVariantQueryData,
  PageVariantCheckQueryData,
  WidgetMountingQueryData,
  UIConfigQueryData,
};
