import * as FetchHttpClient from "@effect/platform/FetchHttpClient";
import * as HttpApi from "@effect/platform/HttpApi";
import * as HttpApiClient from "@effect/platform/HttpApiClient";
import * as HttpApiEndpoint from "@effect/platform/HttpApiEndpoint";
import * as HttpApiGroup from "@effect/platform/HttpApiGroup";
import * as HttpApiSchema from "@effect/platform/HttpApiSchema";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as Model from "../Model.js";
import * as Error from "./Error.js";
/**
 * The dataset namespace parameter.
 */
const datasetNamespace = HttpApiSchema.param("namespace", Model.DatasetNamespace);
/**
 * The dataset name parameter.
 */
const datasetName = HttpApiSchema.param("name", Model.DatasetName);
/**
 * The dataset revision parameter (version, hash, "latest", or "dev").
 */
const datasetRevision = HttpApiSchema.param("revision", Schema.String);
/**
 * The job ID parameter (GET /jobs/{jobId}, DELETE /jobs/{jobId}, PUT /jobs/{jobId}/stop).
 */
const jobId = HttpApiSchema.param("jobId", Model.JobIdParam);
/**
 * The register dataset endpoint (POST /datasets).
 */
const registerDataset = HttpApiEndpoint.post("registerDataset") `/datasets`
    .addError(Error.InvalidPayloadFormat)
    .addError(Error.InvalidManifest)
    .addError(Error.DependencyValidationError)
    .addError(Error.UnsupportedDatasetKind)
    .addError(Error.ManifestRegistrationError)
    .addError(Error.ManifestLinkingError)
    .addError(Error.VersionTaggingError)
    .addError(Error.StoreError)
    .addError(Error.ManifestNotFound)
    .addSuccess(Schema.Void, { status: 201 })
    .setPayload(Schema.Struct({
    namespace: Schema.String.pipe(Schema.propertySignature, Schema.fromKey("namespace")),
    name: Schema.String.pipe(Schema.propertySignature, Schema.fromKey("name")),
    version: Schema.optional(Schema.String).pipe(Schema.fromKey("version")),
    manifest: Model.DatasetManifest,
}));
/**
 * The get datasets endpoint (GET /datasets).
 */
const getDatasets = HttpApiEndpoint.get("getDatasets") `/datasets`
    .addError(Error.DatasetStoreError)
    .addError(Error.MetadataDbError)
    .addSuccess(Model.DatasetsResponse);
/**
 * The get dataset versions endpoint (GET /datasets/{namespace}/{name}/versions).
 */
const getDatasetVersions = HttpApiEndpoint.get("getDatasetVersions") `/datasets/${datasetNamespace}/${datasetName}/versions`
    .addError(Error.InvalidRequest)
    .addError(Error.DatasetStoreError)
    .addError(Error.MetadataDbError)
    .addSuccess(Model.DatasetVersionsResponse);
/**
 * The get dataset by revision endpoint (GET /datasets/{namespace}/{name}/versions/{revision}).
 */
const getDatasetVersion = HttpApiEndpoint.get("getDatasetVersion") `/datasets/${datasetNamespace}/${datasetName}/versions/${datasetRevision}`
    .addError(Error.InvalidRequest)
    .addError(Error.DatasetNotFound)
    .addError(Error.DatasetStoreError)
    .addError(Error.MetadataDbError)
    .addSuccess(Model.DatasetVersionInfo);
/**
 * The deploy dataset endpoint (POST /datasets/{namespace}/{name}/versions/{revision}/deploy).
 */
const deployDataset = HttpApiEndpoint.post("deployDataset") `/datasets/${datasetNamespace}/${datasetName}/versions/${datasetRevision}/deploy`
    .addError(Error.InvalidRequest)
    .addError(Error.DatasetNotFound)
    .addError(Error.DatasetStoreError)
    .addError(Error.SchedulerError)
    .addError(Error.MetadataDbError)
    .addSuccess(Model.DeployResponse, { status: 202 })
    .setPayload(Model.DeployRequest);
/**
 * The get dataset manifest endpoint (GET /datasets/{namespace}/{name}/versions/{revision}/manifest).
 */
const getDatasetManifest = HttpApiEndpoint.get("getDatasetManifest") `/datasets/${datasetNamespace}/${datasetName}/versions/${datasetRevision}/manifest`
    .addError(Error.InvalidRequest)
    .addError(Error.DatasetNotFound)
    .addError(Error.DatasetStoreError)
    .addError(Error.MetadataDbError)
    .addSuccess(Model.DatasetManifest);
/**
 * The get job by ID endpoint (GET /jobs/{jobId}).
 */
const getJobById = HttpApiEndpoint.get("getJobById") `/jobs/${jobId}`
    .addError(Error.InvalidJobId)
    .addError(Error.JobNotFound)
    .addError(Error.MetadataDbError)
    .addSuccess(Model.JobInfo);
/**
 * The output schema endpoint (POST /schema).
 */
const getOutputSchema = HttpApiEndpoint.post("getOutputSchema") `/schema`
    .addError(Error.DatasetStoreError)
    .addError(Error.PlanningError)
    .addError(Error.CatalogForSqlError)
    .addSuccess(Model.OutputSchema)
    .setPayload(Schema.Struct({
    sqlQuery: Schema.String.pipe(Schema.propertySignature, Schema.fromKey("sql_query")),
    isSqlDataset: Schema.Boolean.pipe(Schema.optional, Schema.fromKey("is_sql_dataset")),
}));
/**
 * The api group for the dataset endpoints.
 */
export class DatasetGroup extends HttpApiGroup.make("dataset")
    .add(registerDataset)
    .add(getDatasets)
    .add(getDatasetVersions)
    .add(getDatasetVersion)
    .add(deployDataset)
    .add(getDatasetManifest) {
}
/**
 * The api group for the job endpoints.
 */
export class JobGroup extends HttpApiGroup.make("job")
    .add(getJobById) {
}
/**
 * The api group for the schema endpoints.
 */
export class SchemaGroup extends HttpApiGroup.make("schema")
    .add(getOutputSchema) {
}
/**
 * The api definition for the admin api.
 */
export class Api extends HttpApi.make("admin")
    .add(DatasetGroup)
    .add(JobGroup)
    .add(SchemaGroup) {
}
/**
 * Service definition for the admin api.
 */
export class Admin extends Context.Tag("Amp/Admin")() {
}
/**
 * Creates an admin api service instance.
 *
 * @param url - The url of the admin api service.
 * @returns An admin api service instance.
 */
export const make = Effect.fn(function* (url) {
    const client = yield* HttpApiClient.make(Api, {
        baseUrl: url,
    });
    const registerDataset = Effect.fn("registerDataset")(function* (namespace, name, version, manifest) {
        const request = client.dataset.registerDataset({
            payload: {
                namespace,
                name,
                version: Option.getOrUndefined(version),
                manifest,
            },
        });
        const result = yield* request.pipe(Effect.catchTags({
            HttpApiDecodeError: Effect.die,
            ParseError: Effect.die,
        }));
        return result;
    });
    const getDatasetVersions = Effect.fn("getDatasetVersions")(function* (namespace, name) {
        const result = yield* client.dataset.getDatasetVersions({
            path: {
                namespace,
                name,
            },
        }).pipe(Effect.catchTags({
            HttpApiDecodeError: Effect.die,
            ParseError: Effect.die,
        }));
        return result;
    });
    const getDatasetVersion = Effect.fn("getDatasetVersion")(function* (namespace, name, revision) {
        const result = yield* client.dataset.getDatasetVersion({
            path: {
                namespace,
                name,
                revision,
            },
        }).pipe(Effect.catchTags({
            HttpApiDecodeError: Effect.die,
            ParseError: Effect.die,
        }));
        return result;
    });
    const deployDataset = Effect.fn("deployDataset")(function* (namespace, name, revision, options) {
        const result = yield* client.dataset.deployDataset({
            path: {
                namespace,
                name,
                revision,
            },
            payload: {
                endBlock: options?.endBlock,
                parallelism: options?.parallelism,
            },
        }).pipe(Effect.catchTags({
            HttpApiDecodeError: Effect.die,
            ParseError: Effect.die,
        }));
        return result;
    });
    const getDatasetManifest = Effect.fn("getDatasetManifest")(function* (namespace, name, revision) {
        const result = yield* client.dataset.getDatasetManifest({
            path: {
                namespace,
                name,
                revision,
            },
        }).pipe(Effect.catchTags({
            HttpApiDecodeError: Effect.die,
            ParseError: Effect.die,
        }));
        return result;
    });
    const getDatasets = Effect.fn("getDatasets")(function* () {
        const result = yield* client.dataset.getDatasets({}).pipe(Effect.catchTags({
            HttpApiDecodeError: Effect.die,
            ParseError: Effect.die,
        }));
        return result;
    });
    const getJobById = Effect.fn("getJobById")(function* (jobId) {
        const result = yield* client.job.getJobById({
            path: {
                jobId,
            },
        }).pipe(Effect.catchTags({
            HttpApiDecodeError: Effect.die,
            ParseError: Effect.die,
        }));
        return result;
    });
    const getOutputSchema = Effect.fn("getOutputSchema")(function* (sql, options) {
        const request = client.schema.getOutputSchema({
            payload: {
                sqlQuery: sql,
                isSqlDataset: options?.isSqlDataset ?? true,
            },
        });
        const result = yield* request.pipe(Effect.catchTags({
            ParseError: Effect.die,
            HttpApiDecodeError: Effect.die,
        }));
        return result;
    });
    return {
        registerDataset,
        getDatasets,
        getDatasetVersions,
        getDatasetVersion,
        deployDataset,
        getDatasetManifest,
        getJobById,
        getOutputSchema,
    };
});
/**
 * Creates a layer for the admin api service.
 *
 * @param url - The url of the admin api service.
 * @returns A layer for the admin api service.
 */
export const layer = (url) => make(url).pipe(Layer.effect(Admin), Layer.provide(FetchHttpClient.layer));
//# sourceMappingURL=Admin.js.map