import type { Mutable } from "@zarrita/storage"; import { Array, Group, Location } from "./hierarchy.js"; import type { ArrayMetadata, Attributes, CodecMetadata, DataType, GroupMetadata, Scalar, } from "./metadata.js"; import { jsonEncodeObject } from "./util.js"; interface CreateGroupOptions { attributes?: Record; } interface CreateArrayOptions { shape: number[]; chunkShape: number[]; dtype: Dtype; codecs?: CodecMetadata[]; fillValue?: Scalar; chunkSeparator?: "." | "/"; dimensionNames?: string[]; attributes?: Attributes; } export async function create< Store extends Mutable, _Dtype extends DataType = DataType, >(location: Location | Store): Promise>; export async function create< Store extends Mutable, _Dtype extends DataType = DataType, >( location: Location | Store, options: CreateGroupOptions, ): Promise>; export async function create( location: Location | Store, options: CreateArrayOptions, ): Promise>; export async function create( location: Location | Store, options: CreateArrayOptions | CreateGroupOptions = {}, ): Promise | Group> { let loc = "store" in location ? location : new Location(location); if ("shape" in options) { let arr = await createArray(loc, options); return arr as Array; } return createGroup(loc, options); } async function createGroup( location: Location, options: CreateGroupOptions = {}, ): Promise> { let metadata = { zarr_format: 3, node_type: "group", attributes: options.attributes ?? {}, } satisfies GroupMetadata; await location.store.set( location.resolve("zarr.json").path, jsonEncodeObject(metadata), ); return new Group(location.store, location.path, metadata); } async function createArray( location: Location, options: CreateArrayOptions, ): Promise> { let metadata = { zarr_format: 3, node_type: "array", shape: options.shape, data_type: options.dtype, chunk_grid: { name: "regular", configuration: { chunk_shape: options.chunkShape, }, }, chunk_key_encoding: { name: "default", configuration: { separator: options.chunkSeparator ?? "/", }, }, codecs: options.codecs ?? [], fill_value: options.fillValue ?? null, dimension_names: options.dimensionNames, attributes: options.attributes ?? {}, } satisfies ArrayMetadata; await location.store.set( location.resolve("zarr.json").path, jsonEncodeObject(metadata), ); return new Array(location.store, location.path, metadata); }