var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { debug } from './debug.js';
const moduleLog = debug.extend('package');
export class Package {
    constructor(options) {
        const log = moduleLog.extend(this.name);
        log({ options });
        this.deep = options.deep;
        this.name = options.name;
    }
    createEntity(name) {
        const log = moduleLog.extend(this.createEntity.name);
        log({ name });
        const result = {
            id: () => __awaiter(this, void 0, void 0, function* () {
                const log = moduleLog.extend(this.id.name);
                const result = yield this.id(name);
                log({ result });
                return result;
            }),
            idLocal: () => {
                return this.idLocal(name);
            },
            name: name
        };
        log({ result });
        return result;
    }
    id(...names) {
        return __awaiter(this, void 0, void 0, function* () {
            const log = moduleLog.extend(this.id.name);
            log({ names });
            const deepIdArgs = [this.name, ...names];
            log({ deepIdArgs });
            const result = yield this.deep.id(...deepIdArgs);
            log({ result });
            return result;
        });
    }
    idLocal(...names) {
        const log = moduleLog.extend(this.idLocal.name);
        log({ names });
        const deepIdLocalArgs = [this.name, ...names];
        log({ deepIdLocalArgs });
        const result = this.deep.idLocal(...deepIdLocalArgs);
        log({ result });
        return result;
    }
    applyMiniLinks() {
        return __awaiter(this, void 0, void 0, function* () {
            const log = moduleLog.extend(this.applyMiniLinks.name);
            const { data: packageLinks } = yield this.deep.select({
                up: {
                    tree_id: {
                        _id: ["@deep-foundation/core", 'containTree']
                    },
                    parent_id: {
                        _id: [this.name]
                    }
                }
            });
            log({ packageLinks });
            if (!packageLinks) {
                throw new Error(`Package with name ${this.name} is not found`);
            }
            const result = this.deep.minilinks.apply(packageLinks);
            log({ result });
            return result;
        });
    }
}
//# sourceMappingURL=package.js.map