/**
 * 流程节点 type 判定（含历史数据：isolation / releaseIsolation 及中文旧值）
 */

export function isWorkflowLockSchemeType(t: string | undefined | null): boolean {
  return t === 'lock' || t === 'isolation' || t === '隔离' || t === '隔离/方案';
}

export function isWorkflowUnlockSchemeType(t: string | undefined | null): boolean {
  return t === 'unlock' || t === 'releaseIsolation' || t === '解除隔离';
}

export function isWorkflowCoLockType(t: string | undefined | null): boolean {
  return t === 'coLock' || t === '共锁';
}

export function isWorkflowUnlockCoLockType(t: string | undefined | null): boolean {
  return t === 'unlockCoLock' || t === '解除共锁';
}

/** 与「上锁」节点相同的校验与配置结构（含共锁） */
export function isWorkflowLockLikeValidationType(t: string | undefined | null): boolean {
  return isWorkflowLockSchemeType(t) || isWorkflowCoLockType(t);
}

/** 与「解锁」节点相同的校验结构（含解除共锁） */
export function isWorkflowUnlockLikeValidationType(t: string | undefined | null): boolean {
  return isWorkflowUnlockSchemeType(t) || isWorkflowUnlockCoLockType(t);
}

/** 共锁、解除共锁：业务提交表单不强制（右侧面板不标必填，与作业流程校验一致） */
export function isWorkflowCoLockFamilySubmitFormOptional(t: string | undefined | null): boolean {
  return isWorkflowCoLockType(t) || isWorkflowUnlockCoLockType(t);
}

/** 共锁/解除共锁仅随「上锁挂牌」存在，流程 JSON 与接口 isolationType 固定为字典值 1（字符串） */
export const WORKFLOW_CO_LOCK_FAMILY_ISOLATION_TYPE = '1';

/** 共锁族节点落库/导出时的 isolationType，其余类型保持 current */
export function normalizeIsolationTypeForCoLockFamily(
  nodeType: string | undefined | null,
  current: string | undefined | null
): string {
  if (isWorkflowCoLockType(nodeType) || isWorkflowUnlockCoLockType(nodeType)) {
    return WORKFLOW_CO_LOCK_FAMILY_ISOLATION_TYPE;
  }
  return current != null && String(current) !== '' ? String(current) : '';
}

/**
 * 作业流程管理（IsolationWork）右侧面板：节点信息里出现「负责人」选择框时是否必填（与 UI 红星一致）。
 * 含：确认、审核、录入、还锁、完成；上锁方案里盲板/拆除的负责人仍由 lock 分支按 isolationType 单独校验。
 */
export function isWorkflowJobFlowResponsibleRequired(paletteType: string | undefined | null): boolean {
  if (!paletteType) return false;
  return (
    paletteType === 'confirm' ||
    paletteType === 'review' ||
    paletteType === 'inputInfo' ||
    paletteType === 'returnLock' ||
    paletteType === 'complete'
  );
}

/** 右侧配置：隔离方式、隔离点、关联方案节点等整块 UI */
export function isWorkflowIsolationSchemePanelType(t: string | undefined | null): boolean {
  return (
    isWorkflowLockSchemeType(t) ||
    isWorkflowUnlockSchemeType(t) ||
    isWorkflowCoLockType(t) ||
    isWorkflowUnlockCoLockType(t)
  );
}

/** 画布/持久化中的 type 映射到当前左侧模板节点 type（用于 nodeConfigs 来查找） */
export function resolveWorkflowPaletteType(t: string | undefined | null): string {
  if (!t) return '';
  if (t === 'isolation' || t === '隔离' || t === '隔离/方案') return 'lock';
  if (t === 'releaseIsolation' || t === '解除隔离') return 'unlock';
  if (t === '共锁') return 'coLock';
  if (t === '解除共锁') return 'unlockCoLock';
  return t;
}

/** 解除类节点关联的「方案源」节点 type 是否匹配 */
export function isWorkflowUnlockParentMatch(
  unlockNodeType: string | undefined | null,
  parentNodeType: string | undefined | null
): boolean {
  if (isWorkflowUnlockCoLockType(unlockNodeType)) {
    return isWorkflowCoLockType(parentNodeType);
  }
  if (isWorkflowUnlockSchemeType(unlockNodeType)) {
    return isWorkflowLockSchemeType(parentNodeType);
  }
  return false;
}
