/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
// ============================================================================
// OLD API - Kept for backward compatibility
// ============================================================================
export const asDatasource = (s) => {
    return {
        id: s.uid,
        variant: s.variant_s,
        name: s.name_s,
        description: s.description_t,
        database: s.database_s,
        outputBucket: s.output_bucket_s,
    };
};
// ============================================================================
// New API Types and DTO
// ============================================================================
import { validateJSON } from '../api/utils/validation.js';
/**
 * Datasource domain model for the Datalayer Client.
 * Provides state management and operations for datasources.
 *
 * @example
 * ```typescript
 * const datasource = await client.createDatasource({
 *   type: 'Amazon Athena',
 *   name: 'my-athena-datasource',
 *   description: 'Production Athena datasource',
 *   database: 'my_database',
 *   output_bucket: 's3://my-bucket/output/'
 * });
 *
 * await datasource.update({ description: 'Updated description' });
 * await datasource.delete();
 * ```
 */
export class DatasourceDTO {
    /** @internal */
    _data;
    _client;
    _deleted = false;
    /**
     * Create a Datasource instance.
     * @param data - Datasource data from API
     * @param client - Client instance
     */
    constructor(data, client) {
        this._data = data;
        this._client = client;
    }
    // ========================================================================
    // Helper Methods
    // ========================================================================
    _checkDeleted() {
        if (this._deleted) {
            throw new Error(`Datasource ${this._data.name_s} has been deleted and no longer exists`);
        }
    }
    // ========================================================================
    // Properties
    // ========================================================================
    get uid() {
        this._checkDeleted();
        return this._data.uid;
    }
    get type() {
        this._checkDeleted();
        return this._data.type_s;
    }
    get variant() {
        this._checkDeleted();
        return this._data.variant_s;
    }
    get name() {
        this._checkDeleted();
        return this._data.name_s;
    }
    get description() {
        this._checkDeleted();
        return this._data.description_t;
    }
    get database() {
        this._checkDeleted();
        return this._data.database_s;
    }
    get outputBucket() {
        this._checkDeleted();
        return this._data.output_bucket_s;
    }
    get createdAt() {
        this._checkDeleted();
        return this._data.created_at ? new Date(this._data.created_at) : undefined;
    }
    get updatedAt() {
        this._checkDeleted();
        return this._data.updated_at ? new Date(this._data.updated_at) : undefined;
    }
    // ========================================================================
    // Action Methods
    // ========================================================================
    /**
     * Update this datasource.
     * @param updates - Fields to update
     * @returns Updated Datasource instance
     */
    async update(updates) {
        this._checkDeleted();
        const updated = await this._client.updateDatasource(this.uid, updates);
        return updated;
    }
    /**
     * Delete this datasource permanently.
     */
    async delete() {
        this._checkDeleted();
        await this._client.deleteDatasource(this.uid);
        this._deleted = true;
    }
    // ========================================================================
    // Utility Methods
    // ========================================================================
    /**
     * Get datasource data in camelCase format.
     */
    toJSON() {
        this._checkDeleted();
        const obj = {
            uid: this.uid,
            type: this.type,
            name: this.name,
            description: this.description,
            database: this.database,
            outputBucket: this.outputBucket,
            createdAt: this.createdAt,
            updatedAt: this.updatedAt,
        };
        validateJSON(obj, 'Datasource');
        return obj;
    }
    /**
     * Get raw datasource data exactly as received from API.
     */
    rawData() {
        this._checkDeleted();
        return this._data;
    }
    toString() {
        this._checkDeleted();
        return `Datasource(${this.name}, ${this.type})`;
    }
}
