"use strict";
/*
 * Copyright 2020 Unbounded Systems, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = tslib_1.__importStar(require("@adpt/core"));
const lodash_1 = require("lodash");
const util_1 = require("util");
const common_1 = require("./common");
const k8s_observer_1 = require("./k8s_observer");
const manifest_support_1 = require("./manifest_support");
const Resource_1 = require("./Resource");
const defaultProps = {
    metadata: {},
};
/**
 * k8s ConfigMap resource
 *
 * @param propsIn - props for ConfigMap as specifed in {@link k8s.ConfigMapProps}
 *
 * @public
 */
function ConfigMap(propsIn) {
    const { key, config, metadata, binaryData, data, immutable } = propsIn;
    const binaryDataLL = lodash_1.mapValues(binaryData, (v) => util_1.isBuffer(v) ? v.toString("base64") : v);
    return core_1.default.createElement(Resource_1.Resource, { key: key, kind: "ConfigMap", config: config, metadata: metadata, binaryData: binaryDataLL, data: data ? data : {}, immutable: immutable });
}
exports.ConfigMap = ConfigMap;
ConfigMap.defaultProps = defaultProps;
/** @internal */
exports.configMapResourceInfo = {
    kind: "ConfigMap",
    deployedWhen: () => true,
    statusQuery: async (props, observe, buildData) => {
        const obs = await observe(k8s_observer_1.K8sObserver, core_1.gql `
            query ($name: String!, $kubeconfig: JSON!, $namespace: String!) {
                withKubeconfig(kubeconfig: $kubeconfig) {
                    readCoreV1NamespacedConfigMap(name: $name, namespace: $namespace) @all(depth: 100)
                }
            }`, {
            name: manifest_support_1.resourceIdToName(props.key, buildData.id, buildData.deployID),
            kubeconfig: props.config.kubeconfig,
            namespace: common_1.computeNamespaceFromMetadata(props.metadata)
        });
        return obs.withKubeconfig.readCoreV1NamespacedConfigMap;
    },
    makeManifest: (manifest, elem, _deployID) => {
        const { data, binaryData, immutable } = elem.props;
        return Object.assign({}, manifest, { data,
            binaryData,
            immutable });
    }
};
manifest_support_1.registerResourceKind(exports.configMapResourceInfo);
//# sourceMappingURL=ConfigMap.js.map