/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { asUser } from './User.js';
import { asArray } from '../utils/index.js';
import { newUserMock } from './../mocks/models/index.js';
/**
 * Convert the raw space object to {@link ISpace}.
 *
 * @param user Raw space object from DB
 * @returns Space
 */
export const asSpace = (raw_space) => {
    const owner = newUserMock();
    let members = new Array();
    if (raw_space.members) {
        members = asArray(raw_space.members).map(m => {
            const member = asUser(m);
            return member;
        });
    }
    const space = {
        id: raw_space.uid,
        handle: raw_space.handle_s,
        type: 'space',
        variant: raw_space.variant_s,
        name: raw_space.name_t,
        description: raw_space.description_t,
        public: raw_space.public_b,
        members,
        creationDate: new Date(raw_space.creation_ts_dt),
        owner,
        organization: {
            handle: raw_space.handle_s,
        },
        // Preserve raw Solr fields so consumers can access dynamic fields
        // (e.g. attached_agent_pod_name_s for project-agent assignment)
        ...raw_space,
    };
    return space;
};
