package com.starry.admin.modules.clerk.module.enums;

import cn.hutool.core.util.StrUtil;

/**
 * 上架状态【1：上架，0：下架】
 */
public enum ListingStatus {
    LISTED("1"),
    DELISTED("0");

    private final String code;

    ListingStatus(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public boolean matches(String value) {
        return StrUtil.equals(code, value);
    }

    public boolean isListed() {
        return this == LISTED;
    }

    public static ListingStatus fromCode(String value) {
        if (LISTED.matches(value)) {
            return LISTED;
        }
        if (DELISTED.matches(value)) {
            return DELISTED;
        }
        return LISTED;
    }

    public static boolean isListed(String value) {
        return fromCode(value).isListed();
    }

    public static boolean isDelisted(String value) {
        return !isListed(value);
    }

    public static boolean transitionedToDelisted(String newValue, String originalValue) {
        if (StrUtil.isBlank(newValue)) {
            return false;
        }
        return isDelisted(newValue) && !isDelisted(originalValue);
    }
}
