// Copyright 2022 Luca Casonato. All rights reserved. MIT license.
/**
 * Web Content Publisher API Client for Deno
 * =========================================
 * 
 * webcontentpublisher.googleapis.com API, a service for web content publishers.
 * 
 * Docs: https://developers.google.com/news/subscribe
 * Source: https://googleapis.deno.dev/v1/webcontentpublisher:v1.ts
 */

import { auth, CredentialsClient, GoogleAuth, request } from "/_/base@v1/mod.ts";
export { auth, GoogleAuth };
export type { CredentialsClient };

/**
 * webcontentpublisher.googleapis.com API, a service for web content
 * publishers.
 */
export class WebContentPublisher {
  #client: CredentialsClient | undefined;
  #baseUrl: string;

  constructor(client?: CredentialsClient, baseUrl: string = "https://webcontentpublisher.googleapis.com/") {
    this.#client = client;
    this.#baseUrl = baseUrl;
  }

  /**
   * Checks if a user is eligible for free article access.
   *
   * @param name Required. The resource name of the publication. Format: publications/{publication_id}
   */
  async publicationsCheckFreeAccess(name: string, opts: PublicationsCheckFreeAccessOptions = {}): Promise<CheckFreeAccessResponse> {
    const url = new URL(`${this.#baseUrl}v1/${ name }:checkFreeAccess`);
    if (opts.httpReferrer !== undefined) {
      url.searchParams.append("httpReferrer", String(opts.httpReferrer));
    }
    if (opts.uri !== undefined) {
      url.searchParams.append("uri", String(opts.uri));
    }
    const data = await request(url.href, {
      client: this.#client,
      method: "GET",
    });
    return data as CheckFreeAccessResponse;
  }
}

/**
 * Response message for CheckFreeAccess
 */
export interface CheckFreeAccessResponse {
  /**
   * True if free access should be allowed, false otherwise.
   */
  isAllowed?: boolean;
}

/**
 * Additional options for WebContentPublisher#publicationsCheckFreeAccess.
 */
export interface PublicationsCheckFreeAccessOptions {
  /**
   * Required. The HTTP referrer.
   */
  httpReferrer?: string;
  /**
   * Required. The URI of the content.
   */
  uri?: string;
}