"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WORKFLOW_STEP_FIELD_NAMES = void 0;
exports.areWorkflowStepReferenceMetadataFieldsValid = areWorkflowStepReferenceMetadataFieldsValid;
exports.hasRequiredWorkflowOriginReferenceFields = hasRequiredWorkflowOriginReferenceFields;
exports.pickWorkflowOriginReferenceFields = pickWorkflowOriginReferenceFields;
exports.assignCoercedRawWorkflowStepReferenceFields = assignCoercedRawWorkflowStepReferenceFields;
exports.assignCoercedWorkflowStepOptionalFields = assignCoercedWorkflowStepOptionalFields;
exports.assignNormalizedWorkflowStepReferenceFields = assignNormalizedWorkflowStepReferenceFields;
exports.assignNormalizedWorkflowStepOptionalFields = assignNormalizedWorkflowStepOptionalFields;
const RAW_WORKFLOW_STEP_FIELD_NAMES = [
    "id",
    "kind",
    "title",
    "objective",
    "instructions",
    "doneWhen",
    "humanHandoffWhen",
    "dependsOnStepIds",
    "targetTemplateName",
    "callSiteId",
    "sourceTemplateName",
    "sourceTemplateDigest",
    "expansionGroupId",
    "originSubtreeKey",
    "originWorkflowRef",
    "childKey",
    "childIssueRef",
    "suggestedTemplate",
    "bindingHints",
    "inputs",
    "outputs",
    "replacementForStepIds",
    "result",
];
exports.WORKFLOW_STEP_FIELD_NAMES = new Set(RAW_WORKFLOW_STEP_FIELD_NAMES);
const WORKFLOW_STEP_REFERENCE_FIELD_NAMES = [
    "targetTemplateName",
    "callSiteId",
    "sourceTemplateName",
    "sourceTemplateDigest",
    "expansionGroupId",
];
const WORKFLOW_STEP_REFERENCE_METADATA_FIELD_NAMES = [
    ...WORKFLOW_STEP_REFERENCE_FIELD_NAMES,
    "originSubtreeKey",
];
function areWorkflowStepReferenceMetadataFieldsValid(step) {
    return WORKFLOW_STEP_REFERENCE_METADATA_FIELD_NAMES.every((field) => isOptionalNonEmptyString(step[field]));
}
function hasRequiredWorkflowOriginReferenceFields(originWorkflowRef) {
    return WORKFLOW_STEP_REFERENCE_FIELD_NAMES.every((field) => isNonEmptyString(originWorkflowRef[field]));
}
function pickWorkflowOriginReferenceFields(originWorkflowRef) {
    const pickedFields = {};
    for (const field of WORKFLOW_STEP_REFERENCE_FIELD_NAMES) {
        pickedFields[field] = originWorkflowRef[field];
    }
    return pickedFields;
}
function assignCoercedRawWorkflowStepReferenceFields(target, step, optionalFields) {
    assignPresentWorkflowStepReferenceMetadataFields(target, step, isString);
    if (optionalFields.originWorkflowRef !== null) {
        target.originWorkflowRef = optionalFields.originWorkflowRef;
    }
}
function assignCoercedWorkflowStepOptionalFields(target, optionalFields) {
    if (optionalFields.replacementForStepIds !== undefined) {
        target.replacementForStepIds = [...optionalFields.replacementForStepIds];
    }
    if (optionalFields.suggestedTemplate !== undefined) {
        target.suggestedTemplate = optionalFields.suggestedTemplate;
    }
    if (optionalFields.bindingHints !== undefined) {
        target.bindingHints = optionalFields.bindingHints;
    }
}
function assignNormalizedWorkflowStepReferenceFields(target, step) {
    assignPresentWorkflowStepReferenceMetadataFields(target, step, isNonEmptyString);
    if (step.originWorkflowRef) {
        target.originWorkflowRef = step.originWorkflowRef;
    }
}
function assignNormalizedWorkflowStepOptionalFields(target, step) {
    if (step.childKey) {
        target.childKey = step.childKey;
    }
    if (step.childIssueRef !== undefined) {
        target.childIssueRef = step.childIssueRef ?? null;
    }
    if (step.suggestedTemplate !== undefined) {
        target.suggestedTemplate = step.suggestedTemplate;
    }
    if (step.bindingHints) {
        target.bindingHints = step.bindingHints;
    }
    if (step.inputs) {
        target.inputs = [...step.inputs];
    }
    if (step.outputs) {
        target.outputs = [...step.outputs];
    }
    if (step.replacementForStepIds) {
        target.replacementForStepIds = [...step.replacementForStepIds];
    }
}
function assignPresentWorkflowStepReferenceMetadataFields(target, source, shouldAssign) {
    for (const field of WORKFLOW_STEP_REFERENCE_METADATA_FIELD_NAMES) {
        const value = source[field];
        if (shouldAssign(value)) {
            target[field] = value;
        }
    }
}
function isOptionalNonEmptyString(value) {
    return value === undefined || isNonEmptyString(value);
}
function isNonEmptyString(value) {
    return typeof value === "string" && value.length > 0;
}
function isString(value) {
    return typeof value === "string";
}
