package xtools.boot.mask.handle;

import xtools.boot.mask.MaskCustom;
import xtools.boot.mask.anntation.Mask;

/**
 * <p>Title : DefaultMaskHandle</p>
 * <p>Description : DefaultMaskHandle</p>
 * <p>DevelopTools : Idea_x64_v2026.1</p>
 * <p>DevelopSystem : macOS Sequoia 15.7.5</p>
 * <p>Company : org.xujun</p>
 *
 * @author : XuJun
 * @version : 5.0.0
 * @date : 2026/2/4 15:19
 */
public class DefaultMaskHandle implements MaskCustom {

    /**
     * 脱敏处理
     *
     * @param value 待处理的字符串
     * @param mask  脱敏注解
     * @return 脱敏后的字符串
     */
    @Override
    public String mask(String value, Mask mask) {
        final int length = value.length();
        int prefixNoMaskLen = mask.prefixNoMaskLen();
        int suffixNoMaskLen = mask.suffixNoMaskLen();
        if (prefixNoMaskLen > 0 && length <= prefixNoMaskLen) {
            return value;
        }
        if (suffixNoMaskLen > 0 && length <= suffixNoMaskLen) {
            return value;
        }
        int maskLength = length - prefixNoMaskLen - suffixNoMaskLen;
        if (maskLength <= 0) {
            return value;
        }
        return value.substring(0, prefixNoMaskLen) + mask.maskChar().repeat(maskLength) + value.substring(length - suffixNoMaskLen);
    }
}
